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

Jeffrey E. Forcier jforcier at strozllc.com
Fri Jul 22 20:57:08 EDT 2005


On Jul 22, 2005, at 7:46 PM, Michael Hoffman wrote:

> 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
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>

So in other words, __str__() and its ilk are class methods instead of  
instance methods? The documentation appears to contradict this:

(http://python.org/doc/2.4/ref/specialnames.html)
"For instance, if a class defines a method named __getitem__(), and x  
is an instance of this class, then x[i] is equivalent to x.__getitem__ 
(i)."


However, you appear to be correct, and the docs appear to be confused:

class MyClass(object):

         def edit(self):
                 return "I'm in edit mode"

         def setToEdit(self):
                 MyClass.__str__ = self.edit

In [3]: test = MyClass()
In [4]: str(test)
Out[4]: '<__main__.MyClass object at 0x2d21d0>'
In [5]: test.setToEdit()
In [6]: str(test)
Out[6]: "I'm in edit mode"


Of course, this doesn't quite do what I had in mind, and in fact you  
could say it's kinda sloppy, binding an ostensibly static class  
method to an instance method:

In [7]: repr(MyClass.__str__)
Out[7]: '<bound method MyClass.edit of <__main__.MyClass object at  
0x2d21d0>>'


Either way, guess I will have to take another route. Your suggestion  
is what I'd had in mind, and does the job pretty well. I'm still  
attempting to figure out the best approach to my overall problem,  
however, so who knows where I will end up =)

Thanks for the response! If you or anyone else can shed some insight  
on *why* this is the way it is (and why/if the docs are in fact  
incorrect) I'd appreciate further replies. More knowledge == better.

Regards,
Jeff

--
Jeffrey E. Forcier
Junior Developer, Research and Development
Stroz Friedberg, LLC
15 Maiden Lane, 12th Floor
New York, NY 10038
[main]212-981-6540 [direct]212-981-6546
http://www.strozllc.com

This message is for the named person's use only.  It may contain
confidential, proprietary or legally privileged information. No right to
confidential or privileged treatment of this message is waived or lost
by any error in transmission.  If you have received this message in
error, please immediately notify the sender by e-mail or by telephone at
212.981.6540, delete the message and all copies from your system and
destroy any hard copies.  You must not, directly or indirectly, use,
disclose, distribute, print or copy any part of this message if you are
not the intended recipient.




More information about the Python-list mailing list