Dynamically add methods to classes?

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Thu Jun 22 07:12:04 EDT 2000


Matthew Cline wrote in comp.lang.python:
> Is is possible to dynamically add methods to classes in Python?

Of course. And it's as simple as it could be.

class some_class:
   pass
   
instance = some_class()

def some_method(self):
   print "Hello!"
   
some_class.method = some_method

instance.method()


-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  Murphy's Rules, "Stopwatch battles":
   Each turn in Star Fleet Battles works out to one-thirtieth of a
   second. During this time, a ship can maneuver, fire all weapons, and
   send out a boarding party.



More information about the Python-list mailing list