[docs] [issue28612] str.translate needs a mapping example

Christopher Barker report at bugs.python.org
Fri Dec 30 18:40:16 EST 2016

Christopher Barker added the comment:

This all came out of a thread on python-ideas, starting here:


the thread kind of petered out, but it seems there was a kinda-sorta
consensus that we didn't need any new string methods, but rather same notes
in the docs on how to to use .translate() to remove "all but these" was in

And the defaultdict method was proposed as the easiest / most pythonic.

As it happens, I did't live the fact hat defaultdict will build up a
big(ish) dict of Nones for no reason, and thus suggested a NoneDict option:

class NoneDict(dict):
    Dictionary implementation that always returns None when a key is not in
the dict,
    rather than raising a KeyError
    def __getitem__(self, key):
            val = dict.__getitem__(self, key)
        except KeyError:
            val = None
        return val

Though maybe that's a bit much for the docs.

However, in short:

either the defaultdict approach is siple and pythonic enough to be in teh
docs, or we SHOULD add something new to the string object.

(or maybe someone has another nifty pythonic way to do this with the stdlib
that's better than defaultdict?)


On Fri, Dec 30, 2016 at 12:18 PM, Gaurav Tatke <report at bugs.python.org>

> 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 <report at bugs.python.org>
> <http://bugs.python.org/issue28612>
> _______________________________________


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

nosy: +Chris.Barker

Python tracker <report at bugs.python.org>

More information about the docs mailing list