user5415371
user5415371

Reputation:

Python3 and ASCII

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')

Upvotes: 4

Views: 15540

Answers (1)

tripleee
tripleee

Reputation: 189910

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

Upvotes: 5

Related Questions