[Python-Dev] PEP 461 - Adding % and {} formatting to bytes (original) (raw)
Neil Schemenauer nas at arctrix.com
Wed Jan 15 22:24:27 CET 2014
- Previous message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Next message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Glenn Linderman <v+python at g.nevcal.com> wrote:
On 1/15/2014 7:52 AM, Eric V. Smith wrote:
Either that, or we're back to encoding the result of format and accepting that sometimes it might throw errors, depending on the values being passed into format().
That would take us back to Python 2 hell. Please no. I don't like checking for types either, we should have a special method.
Looks like you need to invent formatb to produce only ASCII. Objects that have formatb can be formatted by bytes.format. To avoid coding, it could be possible that formatb might be a callable in which case it is called to get the result, or not a callable, in which case one calls format and converts the result to ASCII, formatb just indicating a guarantee that only ASCII will result.
Just do:
def __formatb__(self, spec):
return MyClass.__format__(self, spec).encode('ascii')
Note that I think it is better to explicitly use the format method rather than using self.format. My reasoning is that a subclass might implement a format that returns non-ASCII characters.
We don't need a special bytes version of str since the %-operator can call formatb with the correct format spec.
Neil
- Previous message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Next message: [Python-Dev] PEP 461 - Adding % and {} formatting to bytes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]