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

ncf nothingcanfulfill at gmail.com
Fri Jul 22 21:49:10 EDT 2005


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 code:

class Whatever:
    ''' Supposed to be inheritable '''
    def __init__(self):
        self.__str__ = self._encode # Dynamically set the __str__ from
superclass(?)

Well, suffice to say, having the class not inherit from object solved
my problem, as I suspect it may solve yours. ;)

I havn't a clue why it acts that way, however, I hope knowledge of my
past experiances helps you also.

Have a GREAT day :)

-Wes




More information about the Python-list mailing list