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

Discussion in 'Python' started by Brian Blais, Jun 27, 2006.

  1. Brian Blais

    Brian Blais Guest

    Thanks for all who replied to this question about replacing a method. I feel a
    little sheepish for not having caught that I have to replace it in the class, not the
    instance, but I have found a very similar problem trying to replace a method using a
    function defined in pyrex. I post all of the code below, but there are several files.

    The main code is:

    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

    #---------------------------------------------------------------------------------


    any ideas why the pyrex function fails?


    thanks,


    bb




    --
    -----------------


    http://web.bryant.edu/~bblais
    Brian Blais, Jun 27, 2006
    #1
    1. Advertising

  2. Re: pyrex functions to replace a method (Re: replace a method in

    Brian Blais wrote:
    > Thanks for all who replied to this question about replacing a method. I
    > feel a little sheepish for not having caught that I have to replace it
    > in the class, not the instance,


    But you *can* replace it on a per-instance basis. It's easy, perfectly
    legal, and can be very convenient.

    <rant>
    I'm very sorry that some poster here try to forcefit Javaish restricted
    OO conception into Python. Free your mind, burn your books, and take
    full advantage of Python's powerfull object model.
    </rant>


    > but I have found a very similar problem
    > trying to replace a method using a function defined in pyrex. I post
    > all of the code below, but there are several files.
    >
    > The main code is:
    >
    > 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


    Note that - from a purely technical POV - you don't need to define the
    updateXXX methods in This. You can add methods directly - in fact,
    defining a function in a class statement will end up doing the same
    thing as binding it to the class objet outside the class statement.

    > 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)


    (snip)
    >
    > any ideas why the pyrex function fails?
    >


    I don't have much knowledge wrt/ pyrex, but I guess that pyrex functions
    don't implement the descriptor protocol as pure Python functions do, so
    the instance object is not passed to the function at calltime.


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
    Bruno Desthuilliers, Jun 27, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,581
    Victor Bazarov
    Jun 21, 2004
  2. =?iso-8859-1?Q?Fran=E7ois?= Pinard

    Re: Pyrex without Python (was Re: calling Pyrex results from C)

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Jan 21, 2004, in forum: Python
    Replies:
    3
    Views:
    303
    A.M. Kuchling
    Jan 21, 2004
  3. Chris Lambacher
    Replies:
    0
    Views:
    666
    Chris Lambacher
    Jun 8, 2005
  4. TPJ
    Replies:
    1
    Views:
    286
    Diez B. Roggisch
    Nov 9, 2005
  5. Greg Ewing
    Replies:
    2
    Views:
    334
    Dieter Maurer
    Jun 29, 2006
Loading...

Share This Page