Reusing object methods?

Peter Otten __peter__ at web.de
Fri Apr 29 15:52:15 EDT 2005


Ivan Voras wrote:

> I want to 'reuse' the same method from class A in class B, and without
> introducing a common ancestor for both of them - is this possible in an
> elegant, straightforward way?

Straightforward, if not elegant:

>>> def method(self, params):
...     print self, params
...
>>> class A:
...     method = method
...
>>> class B:
...     method = method
...
>>> A().method(42)
<__main__.A instance at 0x402aadac> 42
>>> B().method(42)
<__main__.B instance at 0x402aadac> 42
>>>

Peter




More information about the Python-list mailing list