[Python-3000] More PEP 3101 changes incoming

Ron Adam rrr at ronadam.com
Sat Aug 11 05:50:02 CEST 2007



Greg Ewing wrote:
> Ron Adam wrote:
>> I'm not sure what you mean by "ditch all of these".
> 
> I was guessing that what's meant by returning a
> (value, format_spec) tuple is to re-try the
> formatting using the new value and spec. That's
> what I thought was unnecessary, since the method
> can do that itself if it wants.

It's not retrying because it hasn't tried yet.  As you noted below I think.

It lets the __format__ method do its thing first and then depending on what 
it gets back, it (the format function) may or may not do any formatting.

It's handy to pass both the format specifier and the value both times as 
the __format__ function may only alter one or the other.


>> The output in this case is a null string. Setting the format spec to 
>> None tell the format() function not to do anything more to it.
> 
> Then why not just return an empty string?

Because an empty string is a valid string.  It can be expanded to a minimum 
width which me may not want to do.

Now if returning a single value was equivalent to returning ('', None) or 
('', ''), then that would work.

The format function could check for that case.


>> The format function would first call the objects __format__ method and 
>> give it a chance to have control, and depending on what is returned, try 
>> to handle it or not.
> 
> Okay, I see now -- your format function has more smarts
> in it than mine.

Yes, enough so that you can either *not have* a __format__ method as the 
default, or supply "object" with a very simple generic one.  Which is a 
very easy way to give all objects their __format__ methods.

In the case of not having one, format would check for it.  In the case of a 
vary simple one that doesn't do anything, it just gets back what it sent, 
or possibly a NotImplemented exception.

Which ever strategy is faster should probably be the one chosen here.

> But as was suggested earlier, returning NotImplemented
> ought to be enough to signal a fallback to a different
> strategy.

That isn't the case we're referring to, it's the case where we want to 
suppress a fall back choice that's available.

Cheers,
    Ron



More information about the Python-3000 mailing list