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

2005-07-24 Thread Jeffrey E. Forcier
On Jul 24, 2005, at 5:00 AM, Bengt Richter wrote: > Actually, it's not just the "magic" methods. [...] Thanks for the clarification/explanation =) > This looks a little strange because the repr of the bound method > includes a repr of the thing bound to, > which returns the Edit/View presentat

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

2005-07-23 Thread Bengt Richter
On Sat, 23 Jul 2005 10:59:56 -0400, "Jeffrey E. Forcier" <[EMAIL PROTECTED]> wrote: >Thanks for all the additional replies thus far! > >Apparently the issue, as stated implicitly or explicitly by most of >you, is that new-style class instances essentially defer their magic >methods to the cla

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

2005-07-23 Thread Christopher Subich
Paolino wrote: > Little less ugly: > In [12]:class A(object): >: def __str__(self):return self.__str__() >: def str(self):return 'ciao' >: def setStr(self):self.__str__=self.str >: > > In [13]:a=A() > > In [14]:a.setStr() > > In [15]:str(a) > Out[15]:'

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

2005-07-23 Thread Jeffrey E. Forcier
Thanks for all the additional replies thus far! Apparently the issue, as stated implicitly or explicitly by most of you, is that new-style class instances essentially defer their magic methods to the class's static versions of same. This is good to know :) Ironically, the reason I'm using new

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

2005-07-23 Thread Scott David Daniels
Jeffrey E. Forcier wrote: > ... > 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 setEdit(self): > MyClass.__str__ = self.edit > ... > Either way, gues

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

2005-07-23 Thread Paolino
Little less ugly: In [12]:class A(object): : def __str__(self):return self.__str__() : def str(self):return 'ciao' : def setStr(self):self.__str__=self.str : In [13]:a=A() In [14]:a.setStr() In [15]:str(a) Out[15]:'ciao' The point is str(ob) builtin l

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

2005-07-22 Thread Christopher Subich
ncf wrote: > Well, suffice to say, having the class not inherit from object solved > my problem, as I suspect it may solve yours. ;) Actually, I did a bit of experimenting. If the __str__ reassignment worked as intended, it would just cause an infinite recursion. To paste the class definition a

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

2005-07-22 Thread ncf
In trying to develop a protocol for a current app I'm working on, I was using classes which inherited from object for my core packet, and using str(Message) to convert it to an encoded packet. However, I found that this did not work, and it drove me insane, so in a test file, I wrote the following

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

2005-07-22 Thread Jeffrey E. Forcier
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

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

2005-07-22 Thread Michael Hoffman
Jeffrey E. Forcier wrote: > In other words, str() is _NOT_ apparently calling .__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__(

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

2005-07-22 Thread Jeffrey E. Forcier
I am attempting to write a class whose string representation changes in response to external stimuli. While that effect is obviously possible via other means, I attempted this method first and was surprised when it didn't work, so I now want to know why :) Given the following class definitio