Antoine Pitrou added the comment:

> Whether b''.format() would have to lookup and call __format__ remains
> to be seen. From what I've read, maybe baking in knowledge of bytes,
> float, and int would be good enough. I suspect there might be some
> need for datetimes, but I could be wrong.

The __bytes__ method (and/or tp_buffer) may be a better discriminator than
__format__. It would also allow combining arbitrary buffer objects without
making tons of copies.
What it also means is that "format()" may not be the best method name for
this. It is less about formatting than about combining.

Also, it's not obvious what "formatting" a number as bytes should do.
Should it mimick the bytes constructor:

>>> bytes(5)
b'\x00\x00\x00\x00\x00'

Should it mimick the int to_bytes() method:

>>> (5).to_bytes(4, 'little')
b'\x05\x00\x00\x00'

Numbers currently don't have a __bytes__ method:

>>> (5).__bytes__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute '__bytes__'

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue3982>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to