[New-bugs-announce] [issue28612] str.translate needs a mapping example

Jim Jewett report at bugs.python.org
Fri Nov 4 13:02:34 EDT 2016


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 https://mail.python.org/pipermail/python-ideas/2016-November/043501.html

"""
So really translate with defaultdict is a specialized loop that
marries an algorithmic body (which could do things like look up the
original script or other character properties to decide on the
replacement for the generic case) with a (usually "small") table of
exceptions.  That seems like inspired design to me.
"""

Alas, while inspired, it isn't obvious to someone who isn't yet used to the power of python custom classes.

The documentation (such as https://docs.python.org/3/library/stdtypes.html?highlight=translate#str.translate ) should include such an example.

One possible example would be a defaultdict that says to discard any characters except lower case ASCII lettersI.

----------
assignee: docs at python
components: Documentation
keywords: easy
messages: 280061
nosy: Jim.Jewett, docs at python
priority: normal
severity: normal
stage: needs patch
status: open
title: str.translate needs a mapping example
type: enhancement
versions: Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28612>
_______________________________________


More information about the New-bugs-announce mailing list