Re: "Aliasing" an object's __str__ to a different method
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

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

From: Michael Hoffman <cam.ac.uk@mh391.invalid>
Date: Sat Jul 23 2005 - 01:46:21 CEST

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
Received on Thu Sep 29 17:06:57 2005