2 ms·
Yeah especially in Python 2 you can get into fun messes if you don't really understand what you're doing. Python 2 lets you encode bytes: >>> 'abcd'.encode('as
by greenshackle2 10y ago
Yeah especially in Python 2 you can get into fun messes if you don't really understand what you're doing. Python 2 lets you encode bytes:
>>> 'abcd'.encode('ascii')
'abcd'
And decode unicode:
>>> u'abcd'.decode("ascii")
u'abcd'
It's nonsensical.
Thankfully Python 3 has removed all this madness:
>>> b'abcd'.encode('ascii')
AttributeError: 'bytes' object has no attribute 'encode'
>>> 'abcd'.decode('ascii')
AttributeError: 'str' object has no attribute 'decode'
(For those not familiar, Python 3 switched around the notation for unicode/bytes. In Python 2 "abcd" is a bytes literal, adding u makes it unicode, in Python 3 "abcd" is a unicode literal, adding b makes it bytes.)