4 ms·
This is something I see folks trip on. I think encode/decode are fine names actually. The problem it's that Unicode strings have decode defined at all, and simi
by burntsushi 8y ago
This is something I see folks trip on. I think encode/decode are fine names actually. The problem it's that Unicode strings have decode defined at all, and similarly, that byte strings have encode defined. Byte strings should only have a decode operation and Unicode strings should only have an encode operation. Depending on your input, the wrong operations can actually succeed!
- guitarbill 8y agoNot sure what you're talking about mate, on Python 3.6: >>> "hello world".decode("utf-8") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'str' object has no attribute 'decode' >>> b"hello world".encode("utf-8") Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'bytes' object has no attribute 'encode'
- burntsushi 8y agoThat's good. I guess it's only in Python 2 then.
- luckystarr 8y agoIn Python 2 it works almost the same, except you only get an error when encoding/decoding doesn't work out. So I see this as an improvement.
- dcbadacd 8y agoI'm pretty sure I tripped up on an issue like this on python 3.7 where I double encoded or decoded something. Can't recall the exact case but it was confusing for sure.