5 ms·
Python 3's "bytes" type can't quite decide whether it's an array of small integers or a string. > a = bytes([97,98,99]) > print(a) b'abc' > a[0
by Animats 10y ago
Python 3's "bytes" type can't quite decide whether it's an array of small integers or a string.
> a = bytes([97,98,99])
> print(a)
b'abc'
> a[0]
97
You can use regular expressions on a "bytes" object, as well as most of the string operations, such as ".center()" The "bytes" type in Python 3 still does double duty as an ASCII str/byte class.
- jjawssd 10y agoPython 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