Cheryl Sabella added the comment:
IDLE just added similar functionality to pyparse (issue 32940) using:
class ParseMap(dict):
def __missing__(self, key):
return 120 # ord('x')
# Map all ascii to 120 to avoid __missing__ call, then replace some.
Christopher Barker added the comment:
This all came out of a thread on python-ideas, starting here:
https://mail.python.org/pipermail/python-ideas/2016-October/043284.html
the thread kind of petered out, but it seems there was a kinda-sorta
consensus that we didn't need any new string methods,
Gaurav Tatke added the comment:
Should a user be suggested to use str.translate() for the use case where user
only wants to keep certain characters and strip off everything else?
--
___
Python tracker
Gaurav Tatke added the comment:
Hi,
Pardon my ignorance, I am new to this but have below queries/thoughts -
1. Why would we say that adding new keys during lookup phase is an unpleasant
side-effect? From what I understood by docs, one of the main reasons to use
defaultdicts is to be able to
Serhiy Storchaka added the comment:
If the side effect of defaultdict is unpleasant, the correct way is combining
the translation mapping with the custom mapping by ChainMap. But this example
is too complex for the documentation of str.translate(). On other side, it is
trivial for more
Raymond Hettinger added the comment:
I like the idea of adding a mapping example but don't want to encourage use of
defaultdict in contexts like this one. A defaultdict usefully specifies a
default but has the unpleasant side-effect of altering the dictionary (adding
new keys) during the
Gaurav Tatke added the comment:
Hi,
I am new to Python and want to contribute. I am attaching a patch having
required example of using defaultdict with translate. Please let me know if
anything needs to be changed. I have tested the example and also the html doc
in my local.
Regards,
Gaurav
Chris Barker added the comment:
Agreed:
the custom dict type would be nice for a recipe or blog post or...
but not for the docs.
I'll note that the other trick to this recipe is that you need to know to use
lambda to make a "None factory" for defaultdict -- though maybe that's a ToDo
for
Jim Jewett added the comment:
https://mail.python.org/pipermail/python-ideas/2016-November/043539.html by
Chris Barker points out that a custom object (which doesn't ever store the
missing "keys") may be better still... though I'm not sure it is better enough
to complicate the docs.
New submission from Jim Jewett:
One commonly needed string transformation is stripping out certain characters
(or only keeping certain characters). This is common enough that it might be
worth a dedicated method, except, that, as Stephen J. Turnbull wrote in
10 matches
Mail list logo