[Python-Dev] PEP 461 - Adding % and {} formatting to bytes

Neil Schemenauer nas at arctrix.com
Thu Jan 16 16:51:21 CET 2014

Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Neil Schemenauer wrote:
>> Objects that implement __str__ can also implement __bytes__ if they
>> can guarantee that ASCII characters are always returned,
> I think __ascii_ would be a better name. I'd expect
> a method called __bytes__ on an int to return some
> version of its binary value.

I realize now we can't use __bytes__.  Currently, passing an int
to bytes() causes it to construct an object with that many null

If we are going to support format() (I'm not convinced it is nessary
and could easily be added in a later version), then we need an
equivalent to __format__.  My vote is either:

    def __formatascii__(self, spec):


    def __ascii__(self, spec):

Previously I was thinking of __bformat__ or __formatb__ but having
ascii in the method name is a great reminder.

Objects with a natural arbitrary byte representation can implement
__bytes__ and %s should use that if it exists.


More information about the Python-Dev mailing list