This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients Henning.von.Bargen, benjamin.peterson, docs@python, martin.panter, pitrou, r.david.murray, serhiy.storchaka
Date 2015-02-20.13:37:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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)
>>> bytes(b)  # Actually 4 bytes = 2 items × 2 bytes
>>> 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()

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:46martin.pantersetrecipients: + martin.panter, pitrou, benjamin.peterson, r.david.murray, docs@python, serhiy.storchaka, Henning.von.Bargen
2015-02-20 13:37:46martin.pantersetmessageid: <>
2015-02-20 13:37:46martin.panterlinkissue20699 messages
2015-02-20 13:37:46martin.pantercreate