[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:32:19 CEST 2006


Hi Brian,

Brian Blais wrote:
> import module_py   # import a function from a python module
> import module_pyrex # import a function from a pyrex extension module
> 
> class This(object):
> 
>      def update1(self,val):
>          print val
> 
>      def update2(self,val):
>          print "2",val
> 
>      def update3(self,val):
>          print "3",val
> 
> def local_update(obj,val):
> 
>      print "local",val
> 
> 
> This.update1=local_update  # replace the method from a local function
> This.update2=module_py.python_update  # replace the method from a python module
> This.update3=module_pyrex.pyrex_update  # replace the method from a pyrex module
> 
> t=This()
> 
> t.update1('local')  # works fine
> t.update2('python') # works fine
> t.update3('pyrex')  # gives a typeerror function takes exactly 2 arguments (1 given)
> #---------------------------------------------------------------------------------
> 
> #module_py.py
> 
> def python_update(self,val):
>      print "python module",val
> #---------------------------------------------------------------------------------
> 
> #module_pyrex.pyx
> 
> def pyrex_update(self,val):
>      print "pyrex module",val

What is the 'self' for? After all, you are sticking a /function/ into the
object, not a /method/. Your function will not receive a 'self' argument
automatically as it is stuck into the instance and not part of the class.

Stefan




More information about the Python-list mailing list