[Python-Dev] Adding __format__ to classic classes

Eric Smith eric+python-dev at trueblade.com
Wed Feb 13 18:48:25 CET 2008


Guido van Rossum wrote:
> On Feb 13, 2008 5:28 AM, Eric Smith <eric+python-dev at trueblade.com> wrote:
>> When backporting PEP 3101, do we want to add __format__ to classic
>> classes?  If so, could someone give me a pointer on how to implement
>> this?  I don't see where to hook it up.
> 
> You just have to get the '__format__' attribute and call it if it
> exists. Isn't that how you do it for new-style classes too?
> 

I'm thinking that I need to add a __format__ to the "most base" old 
style class, similar to how I added it for object itself (in 
object_methods[]).  As I currently have it in 2.6, I can call __format__ 
on a new style class, but not a classic class:

$ ./python.exe
Python 2.6a0 (trunk:60757M, Feb 13 2008, 09:14:18)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

 >>> class newstyle(object): pass
...
 >>> class oldstyle: pass
...

 >>> newstyle().__format__('')
'<__main__.newstyle object at 0x3d4d90>'

 >>> oldstyle().__format__('')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: oldstyle instance has no attribute '__format__'

 >>>

So my question is, to what do I need to add __format__ so that classic 
classes will have a default implementation?

My knowledge of how classic classes are implemented is weak, so I don't 
know where to add this.

Eric.




More information about the Python-Dev mailing list