[Python-3000] tp_bytes and __bytes__ magic method

Christian Heimes lists at cheimes.de
Thu Aug 9 01:49:35 CEST 2007


Guido van Rossum wrote:
>> I can think of a bunch of use cases for a magic method.
> 
> Such as?

The __bytes__ method could be used to implement a byte representation of
an arbitrary object. The byte representation can then be used to submit
the object over wire or dump it into a file. In Python 2.x I could
overwrite __str__ to send an object over a socket but in Python 3k str()
returns a unicode object that can't be transmitted over sockets. Sockets
support bytes only.

Christian



More information about the Python-3000 mailing list