This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str.translate() behaves differently for ASCII-only and other strings
Type: enhancement Stage: resolved
Components: Documentation Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, serhiy.storchaka, sir-sigurd, terry.reedy
Priority: normal Keywords:

Created on 2019-02-22 05:34 by sir-sigurd, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg336279 - (view) Author: Sergey Fedoseev (sir-sigurd) * Date: 2019-02-22 05:34
In [186]: from itertools import cycle

In [187]: class ContainerLike:
     ...:     def __init__(self):
     ...:         self.chars = cycle('12')
     ...:     def __getitem__(self, key):
     ...:         return next(self.chars)
     ...: 

In [188]: 'aaaaaa'.translate(ContainerLike())
Out[188]: '111111'

In [189]: 'ыыыыыы'.translate(ContainerLike())
Out[189]: '121212

It seems that behavior was changed in https://github.com/python/cpython/commit/89a76abf20889551ec1ed64dee1a4161a435db5b. At least it should be documented.
msg336280 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-02-22 06:11
You are using a mapping that returns different values for the same key. You should not expect a stable result for it.

I do not think this needs a special mentioning in the documentation. Garbage in -- garbage out.
msg336893 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2019-03-01 04:52
I agree.  The fact the CPython attempts to optimize all_ascii.translate(table) is a CPython implementation detail, not a language feature.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80253
2019-03-01 04:52:36terry.reedysetstatus: open -> closed

type: enhancement
assignee: docs@python
components: + Documentation

nosy: + terry.reedy, docs@python
messages: + msg336893
resolution: not a bug
stage: resolved
2019-02-22 06:11:47serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg336280
2019-02-22 05:44:14sir-sigurdsettitle: str.translate() behave differently for ASCII-only and other strings -> str.translate() behaves differently for ASCII-only and other strings
2019-02-22 05:34:29sir-sigurdcreate