[issue20699] Document that binary IO classes work with bytes-likes objects
Martin Panter added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue20699> _______________________________________
participants (1)
-
Martin Panter