Redefining class methods

Greg Landrum glandrum at my-deja.com
Fri Aug 25 10:38:17 EDT 2000


In article <8o4gf0$12c$1 at nnrp1.deja.com>,
  shochet at my-deja.com wrote:
> Is there an easy way (maybe within emacs mode) to redefine a class
> method without redefining the class? The problem with completely
> redefining the entire class is that all the objects created with the
> old class are now stale.
>

If I understand what you are asking, you just need to assign some
other method to the relevant method name *in the class*.  So, for
instance, this code changes the foo() method of class A(), which
alters already instantiated objects as well as anything created
in the future:
#---------------
class A:
    def foo(self):
        print 'foo'


def bar(self):
    print 'bar'

obj1 = A()
obj1.foo()
A.foo = bar
obj1.foo()
obj2 = A()
obj2.foo()
#---------------

I hope this (a) helps, (b) answers the right question.
-greg




Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list