Is there a reason not to do this?

Ron Garret rNOSPAMon at flownet.com
Thu Nov 30 14:58:52 EST 2006


One of the things I find annoying about Python is that when you make a 
change to a method definition that change is not reflected in existing 
instances of a class (because you're really defining a new class when 
you reload a class definition, not actually redefining it).  So I came 
up with this programming style:

def defmethod(cls):
  return lambda (func): type.__setattr__(cls, func.func_name, func)

class c1(object): pass

@defmethod(c1)
def m1(self, x): ...


Now if you redefine m1, existing instances of c1 will see the change.

My question is: is there a reason not to do this?  Does it screw 
something up behind the scenes?  Is it unpythonic?  Why isn't this 
standard operating procedure?

rg



More information about the Python-list mailing list