replace a method in class: how?

Maric Michaud maric at aristote.info
Mon Jun 26 19:41:32 EDT 2006


Le mardi 27 juin 2006 01:14, Brian Blais a écrit :
> t=This()
> t.update(5)
> t.update=another_update
> t.update(5)  # this one doesn't work, gives
> # TypeError: another_update() takes exactly 2 arguments (1 given)
>
>
> clearly it isn't seeing it as a method, just an attribute which happens to
> be a function.  Is there a preferred way to do this?

Yes, this is because you assigned it to the instance, not the class, it should 
be :
t=This()
t.update(5)
This.update=another_update
t.update(5)

In OOP Methods are defined in *classes* not in any arbitrary object (ie. 
instances). You can learn more on how it works in python reading about the 
descriptor protocol of new style class (__getattribute__ special method).

To clearly understand what belongs to class and what belongs to instances, 
try :
u=This()
t.prop = None
t.prop
u.prop
This.other_prop = None
t.other_prop, u.other_prop 
This.__dict__.items()
t.__dict__, u.__dict__


-- 
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097



More information about the Python-list mailing list