471,342 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

adding methods at runtime

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.
Jan 10 '08 #1
3 3960
On Thu, 10 Jan 2008 14:55:18 -0800, zs****@gmail.com wrote:
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
Jan 10 '08 #2
On Jan 11, 10:44 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Thu, 10 Jan 2008 14:55:18 -0800, zsl...@gmail.com wrote:
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:
>>import types, new; types.MethodType is new.instancemethod
True
>>>

Jan 11 '08 #3
zs****@gmail.com 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
Jan 11 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Fernando Rodriguez | last post: by
2 posts views Thread by manohar.shankar | last post: by
5 posts views Thread by Simon Tamman {Uchiha Jax} | last post: by
9 posts views Thread by Mike | last post: by
3 posts views Thread by Udi | last post: by
318 posts views Thread by King Raz | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.