[Python-Dev] PEP 461 - Adding % and {} formatting to bytes (original) (raw)

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 bytes.

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):
    ...

or

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.

Neil



More information about the Python-Dev mailing list