adding methods at runtime

Z

zslevi

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.
 
M

Marc 'BlackJack' Rintsch

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

[Code snipped]

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

You are not adding a method but a function. Take a look at
`types.MethodType()` to create a method from a function, instance, and
class.

Ciao,
Marc 'BlackJack' Rintsch
 
J

John Machin

Can I access the class attributes from a method added at runtime? (My
experience says no.)
I experimented with the following code:
[Code snipped]
So it seems to me, if you add a method to an instance, the method will
not get "self" as parameter.

You are not adding a method but a function. Take a look at
`types.MethodType()` to create a method from a function, instance, and
class.

Just in case gentle readers are wondering where to find the docs for
types.MethodType, here's a hint:
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
Can I access the class attributes from a method added at runtime?

Of course.
(My
experience says no.)

So there's something wrong with your experience !-)
I experimented with the following code:


class myclass(object):
myattr = "myattr"

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

instance.method = method

As Marc pointed out, you're not adding a method but a function. What you
want is:

def method(self, x):
print "x : %s - myattr : %s" % (x, self.myattr)

import new
instance.method = new.instancemethod(method, instance, myclass)

Note that this is only needed for per-instance methods - if you want to
add a new method for all instances, you just set the function as
attribute of the class. The lookup mechanism will then invoke the
descriptor protocol on the function object, which will return a method
(FWIW, you have to do it manually on per-instance methods because the
descriptor protocol is not invoked on instance attributes, only on class
attributes).

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top