"Aliasing" an object's __str__ to a different method

Michael Hoffman cam.ac.uk at mh391.invalid
Fri Jul 22 19:46:21 EDT 2005


Jeffrey E. Forcier wrote:

> In other words, str() is _NOT_ apparently calling <object>.__str__ as  
> it's supposed to! However, calling __str__ directly (which, yes,  you're 
> not supposed to do) does work as expected:

I'm too tired and/or lazy to check, but I believe str() is calling 
MyClass.__str__(testObject) as it is supposed to rather than 
testObject.__str__() as you say it is supposed to. ;-)

Someone else or Google can probably enlighten you on the reason for this 
better than I. I think this is something that is not explained very well 
in the docs, which do say that a class implements special method names, 
but doesn't make it very clear that an object can't always override them.

If you redirect to a second method you can overwrite on the instance, I 
think you will get the results you want.
-- 
Michael Hoffman



More information about the Python-list mailing list