Add a method to an existing class
bedge at troikanetworks.com
Wed May 16 19:06:12 CEST 2001
I have some idl generated classes. I'd like to add a __repr__ method to
control default output representation.
So, my question is, how does one add a method to an existing class when
you don't have control of the class definition.
>>> class xx:
... def __init__(self):
... self.i = 99
>>> x = xx()
Now, I want to add __repr__ to class xx. I tried the following:
>>> def str(self):
... return "%d" % self.i
It works, but I'm wondering if this is the right way.
More information about the Python-list