adding methods at runtime

zslevi@gmail.com levilista at gmail.com
Thu Jan 10 17:55:18 EST 2008


Can I access the class attributes from a method added at runtime? (My
experience says no.)
I experimented with the following code:


class myclass(object):
    myattr = "myattr"

instance = myclass()
def method(x):
    print x

instance.method = method
instance.method("hello world")

inst2 = myclass()
#inst2.method("inst2")

def meth2(x):
    print x.myattr

myclass.ujmeth = meth2
inst2 = myclass()
inst2.ujmeth()


############
The output:
##########

hello world
myattr

################

So it seems to me, if you add a method to an instance, the method will
not get "self" as parameter.



More information about the Python-list mailing list