Dynamically add methods to classes?

Michael Hudson mwh21 at cam.ac.uk
Thu Jun 22 07:16:34 EDT 2000


Matthew Cline <matt at nightrealms.com> writes:

> Is is possible to dynamically add methods to classes in Python?

Perfectly possible:

>>> class C:
...  def a(self): print "a"
... 
>>> c=C()
>>> def b(self): print "b"
... 
>>> c.a()
a
>>> C.b=b
>>> c.b()
b

Is that what you meant?

Cheers,
M.

-- 
  In many ways, it's a dull language, borrowing solid old concepts
  from many other languages & styles:  boring syntax, unsurprising
  semantics, few  automatic coercions, etc etc.  But that's one of
  the things I like about it.                 -- Tim Peters, 16 Sep 93



More information about the Python-list mailing list