2 ms·
Python 3: In this case print(a) calls a.__str__() which happens to default to calling a.__repr__() because a doesn't have an __str__ attribute. a[0] is equiva
by jjawssd 9y ago
Python 3:
In this case print(a) calls a.__str__() which happens to default to calling a.__repr__() because a doesn't have an __str__ attribute.
a[0] is equivalent to a.__getitem__(0)
That's because the __repr__ of a bytes object instance formats the result into a human readable representation of the data which should ideally be identical to the syntax needed to create the instance, whereas the __getitem__ method returns a "real" and machine usable representation.
Try it...
>>> a
b'abc'
>>> a.__str__()
"b'abc'"
>>> a.__repr__()
"b'abc'"
>>> a.__getitem__(0)
97