[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

adding methods at runtime

levilista@gmail.com

1/10/2008 10:55:00 PM

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.
3 Answers

Marc 'BlackJack' Rintsch

1/10/2008 11:44:00 PM

0

On Thu, 10 Jan 2008 14:55:18 -0800, zslevi@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

John Machin

1/11/2008 10:01:00 AM

0

On Jan 11, 10:44 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net> wrote:
> 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
>>>


Bruno Desthuilliers

1/11/2008 11:01:00 AM

0

zslevi@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