简体   繁体   中英

Why did python-3.x remove ROT-13 as an encoding?

With python-2.7, you can pretty easily implement a rot-13 Ceasar Cipher using

>>> 'abcdefghijklmnopqrstuvwxyz'.encode('rot-13')
'nopqrstuvwxyzabcdefghijklm'

You'll even find it in the Zen of Python code in the CPython repository .

However, the same code on python3.6 gives -

>>> 'abcdefghijklmnopqrstuvwxyz'.encode('rot-13')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'rot-13' is not a text encoding; use codecs.encode() to handle arbitrary codecs

If I want to use the rot-13 encoding in python3.x, I'll need to import codecs .

>>> import codecs
>>> codecs.encode('abcdefghijklmnopqrstuvwxyz', 'rot-13')
'nopqrstuvwxyzabcdefghijklm'

Of course, this is really a minor issue, I don't mind importing codecs to implement a caesar cipher (it's a builtin anyway). I'm just curious to know if there was any underlying rationale behind this design decision. Maybe the reason is as simple as "rot-13 isn't really an encoding", I don't know.

If someone can shed some light on this, I'd love to hear it!

you can also take a look at this page where someone flagged your issue as a bug. For those who don't want to go through the site and its followup links, the simple response from a python committer was as follows:

"Since rot_13 is a transcoder, not an encoder, the error message is correct, as is the fix for the function. However, since neither the module encodings.rot_13 nor the rot13 function in the module are documented, (not even in 2.7), I wonder if the function and the if __name__; clause are ancient holdovers that should be removed."

A quick search for "what's new in python rot-13" turns up this: https://docs.python.org/3/whatsnew/3.4.html#codec-handling-improvements

In Python 3.4, the interpreter is able to identify the known non-text encodings provided in the standard library and direct users towards these general purpose convenience functions when appropriate:

 >>> >>> b"abcdef".decode("hex") Traceback (most recent call last): File "<stdin>", line 1, in <module> LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs >>> "hello".encode("rot13") Traceback (most recent call last): File "<stdin>", line 1, in <module> LookupError: 'rot13' is not a text encoding; use codecs.encode() to handle arbitrary codecs 

So apparently, this was a cleanup operation to separate actual text encodings (which you would use in an open(file, encoding="foo") call) from other encodings.

Python moved the rot13 to (as you said), codecs. My guess is to better reflect what rot13 is and to use a different and more general interface. As TimPietzcker said, most likely cleanup and trying to group similar functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM