Message236282
Using len(b) is fine if b is a bytes() or bytearray() object, but a bytes-like object can be anything that you can pass to memoryview(). In general len(b) is not part of that protocol, so can return some other value, or even be unimplemented:
>>> from array import array
>>> b = array("H", range(2))
>>> len(b)
2
>>> bytes(b) # Actually 4 bytes = 2 items × 2 bytes
b'\x00\x00\x01\x00'
>>> from ctypes import c_int
>>> b = c_int(100)
>>> len(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'c_long' has no len()
>>> bytes(b) # 4 bytes despite not implementing len()
b'd\x00\x00\x00'
I see your point that “the number of bytes in b” can be misleading. I will think about the wording some more. Maybe we can come up with a third alternative, like “the number of bytes given” or something. |
|
Date |
User |
Action |
Args |
2015-02-20 13:37:46 | martin.panter | set | recipients:
+ martin.panter, pitrou, benjamin.peterson, r.david.murray, docs@python, serhiy.storchaka, Henning.von.Bargen |
2015-02-20 13:37:46 | martin.panter | set | messageid: <1424439466.66.0.0273387224544.issue20699@psf.upfronthosting.co.za> |
2015-02-20 13:37:46 | martin.panter | link | issue20699 messages |
2015-02-20 13:37:46 | martin.panter | create | |
|