[Pyrex] pyrex functions to replace a method (Re: replace a method in class: how?)

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Wed Jun 28 13:29:46 CEST 2006



Brian Blais wrote:
> Greg Ewing wrote:
>> Brian Blais wrote:
>>> I have found a very similar problem trying to replace a method using a 
>>> function defined in pyrex.
>>
>> What *should* work is to define the method inside a
>> class in Pyrex (plain class, not extension type) and
>> extract it out of the class's __dict__. That's because
>> Pyrex pre-wraps a function defined in a class in an
>> unbound method object before putting it in the class.
>>
> 
> So I tried:
> 
> #---------------------------------------------------------------------------------
> 
> #module_pyrex.pyx
> 
> class update_funcs:
> 
>      def pyrex_update_within_class(self,val):
>          print "pyrex module within class",val
> 
> 
> #---------------------------------------------------------------------------------
> 
> #(adding to test_replace_method.py)
> 
> This.update4=module_pyrex.update_funcs.__dict__['pyrex_update_within_class']
> 
> t.update4('pyrex within class') # doesn't work
> 
> #---------------------------------------------------------------------------------
> 
> and get:
> 
> TypeError: unbound method pyrex_update_within_class() must be called with 
> update_funcs instance as first argument (got str instance instead)
> 
> did I do this wrong?

Yes. :)

What you got was an unbound method. You can't call unbound methods without
specifying the object you want to call them on (i.e. the 'self' argument).

http://docs.python.org/tut/node11.html#SECTION0011340000000000000000

When you're using 'plain classes', you can do with them whatever you do in
standard Python, so replacing a method is just done with an attribute assignment.

Stefan



More information about the Python-list mailing list