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 <1424439466.66.0.0273387224544.issue20699@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
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: <1424439466.66.0.0273387224544.issue20699@psf.upfronthosting.co.za>
2015-02-20 13:37:46martin.panterlinkissue20699 messages
2015-02-20 13:37:46martin.pantercreate