dynamically extend classes

Moshe Zadka moshez at math.huji.ac.il
Sat Feb 19 14:28:40 EST 2000


On 19 Feb 2000, Matt Wette wrote:

> 
> Is there a way to dynamically extend classes in Python?
> 
> I'd like to be able to have sitecustomize.py import a module
> and add methods to a class so that later, when anohter modules
> imports this module and uses the class it sees the added methods.

sure:

>>> class foo:
...     pass
... 
>>> def tt(self): print "hello world"
... 
>>> a = foo()
>>> 
>>> foo.tt = tt
>>> a.tt()
hello world

Don't do it.

--
Moshe Zadka <mzadka at geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.





More information about the Python-list mailing list