[Python-3000] More PEP 3101 changes incoming

Ron Adam rrr at ronadam.com
Fri Aug 10 06:35:34 CEST 2007



Greg Ewing wrote:
> Ron Adam wrote:
>> The way to think of 'repr' and 'str' is that of a general "object" format 
>> type/specifier.  That puts str and repr into the same context as the rest 
>> of the format types.  This is really a point of view issue and not so much 
>> of a semantic one.  I think {0:r} and {0:s} are to "object", as {0:d} and 
>> {0:e} are to "float" ...  just another relationship relative to the value 
>> being formatted.  So I don't understand the need to treat them differently.
> 
> There's no need to treat 's' specially, but 'r' is different,
> at least if we want
> 
>     "{0:r}".format(x)
> 
> to always mean the same thing as
> 
>     "{0:s}".format(repr(x))
> 
> To achieve that without requiring every __format__ method
> to recognise 'r' and handle it itself is going to require
> format() to intercept 'r' before calling the __format__
> method, as far as I can see. It can't be done by str,
> since by the time str.__format__ gets called, the object
> has already been passed through str(), and it's too late
> to call repr() on it.

This doesn't require a different syntax to do.

Lets start at the top... what will the str.format() method look like?



Maybe an approximation might be:  (only one possible variation)

class str(object):
     ...
     def format(self, *args, **kwds):
         return format(self, *args, **kwds)  #calls global function.
     ...

And then for each format field, it will call the __format__ method of the 
matching position or named value.

class object():
     ...
     def __format__(self, value, format_spec):
        return value, format_spec
     ...

It doesn't actually do anything because it's a pre-format hook so that 
users can override the default behavior.  An overridden __format__ method 
can do one of the following...

     - handle the format spec on it's own and return a (string, None) [*]

     - alter the value and return (new_value, format_spec)

     - alter the format_spec and return (value, new_format_spec)

     - do logging of some values, and return the (value, format_spec) 
unchanged.

     - do something entirely different and return ('', None)

[* None could indicate an already formatted value in this case.]


Does this look ok, or would you do it a different way?

If we do it this way, then the 'r' formatter isn't handled any different 
than any of the others.  The exceptional case is the custom formatters in 
__format__ methods not the 'r' case.

Cheers,
    Ron















More information about the Python-3000 mailing list