I am learning python, and I am a bit confused about contents.encode() in init() in the following code.
PY3 = sys.version_info[0] > 2
class Test:
def __init__(self):
self.contents = ''
if PY3:
self.contents = self.contents.encode('ascii')
Python 3 strings are Unicode strings. There are situations where you want data in a byte string, where (typically) every character is a single byte. "string".encode('ascii') creates a byte string containing the six ASCII characters s, t, r, i, n, g out of the Unicode string containing these characters as Unicode.
This is a portability tweak; Python 2 strings were byte strings (though there is the u"string" notation for creating Unicode strings, starting in Python 2.5 IIRC).
For a richer exposition of the precise difference, perhaps see http://nedbatchelder.com/text/unipain.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With