[Python-Dev] Adding __format__ to classic classes

Eric Smith eric+python-dev at trueblade.com
Wed Feb 13 22:42:11 CET 2008


Guido van Rossum wrote:
>> Much to my surprise, this already works:
>>
>>  >>> format(oldstyle(), '+^50s')
>> '+++++<__main__.oldstyle instance at 0x3d91f8>+++++'
>>  >>>
>>
>> So I guess it's a moot point.  I'm using the same code as I use in 3.0,
>> where I call:
>>    meth = _PyType_Lookup(Py_TYPE(value), format_str);
>> where format_str is "__format__" interned.  And I'm getting a value back
>> for old style classes.
>>
>> Eric.
> 
> But now try overriding __format__().  The 3.0 code uses
> _PyType_Lookup() which is not traversing the class dicts for classic
> classes, so it won't find __format__ overrides.
> 

Okay, I see and understand that issue.  But looking at len or getattr, I 
don't see how to generalize it to __format__.  __len__ and __getattr__ 
have special support in the classes, with cl_getattr, tp_getattr, etc.

I hate to be dense, but could you point me to some code that does 
something similar but looks up the method by name?

Thanks for your time.
Eric.


More information about the Python-Dev mailing list