Re: [Pyrex] pyrex functions to replace a method (Re: replace a method

Discussion in 'Python' started by Greg Ewing, Jun 28, 2006.

  1. Greg Ewing

    Greg Ewing Guest

    Brian Blais wrote:
    > I have found a very similar problem trying to replace a method using a
    > function defined in pyrex.


    Functions defined in Pyrex are C-implemented functions,
    which don't trigger the method binding magic when you
    access them through a class. The same thing happens if
    you try to use a built-in function as a method.

    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.

    I have suggested that builtin functions should be
    given the same method-binding behaviour as interpreted
    functions. The idea wasn't rejected out of hand, but
    I don't think anything has been done about it yet.

    --
    Greg
    Greg Ewing, Jun 28, 2006
    #1
    1. Advertising

  2. Greg Ewing wrote:
    > Brian Blais wrote:
    >
    >> I have found a very similar problem trying to replace a method using a
    >> function defined in pyrex.

    >
    >
    > Functions defined in Pyrex are C-implemented functions,
    > which don't trigger the method binding magic when you
    > access them through a class. The same thing happens if
    > you try to use a built-in function as a method.
    >
    > 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.


    Or write the needed descriptor and wrap the function in (Q&D, needs a
    lot of improvements):

    def CFuncWrapper(func, instance):
    def _wrappedCFunc(*args, **kw):
    return func(instance, *args, **kw)
    return _wrappedCFunc

    class CFuncMethodType(object):
    def __init__(self, func):
    self.func = func

    def __get__(self, instance, cls=None):
    if instance:
    return CFuncWrapper(self.func, instance)
    else:
    assert(cls is not None)
    return self.func

    >>> class Foo(object): pass

    ....
    >>> Foo.isa = CFuncMethodType(isinstance)
    >>> Foo.isa(Foo(), Foo)

    True
    >>> f.isa(list)

    False
    >>> f.isa(Foo)

    True
    >>>



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

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

    Greg Ewing <> writes on Wed, 28 Jun 2006 11:56:55 +1200:
    >...
    > I have suggested that builtin functions should be
    > given the same method-binding behaviour as interpreted
    > functions. The idea wasn't rejected out of hand, but
    > I don't think anything has been done about it yet.


    You can use:

    def wrapAsMethod(to_be_wrapped)
    def wrapped(*args, **kw):
    return to_be_wrapper(*args, **kw)
    return wrapped

    and then use in your class:

    class ...:
    ...
    myMethod = wrapAsMethod(builtin_function)


    Such, this use case probably does not require a language change.


    Dieter
    Dieter Maurer, Jun 29, 2006
    #3
    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,576
    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:
    665
    Chris Lambacher
    Jun 8, 2005
  4. TPJ
    Replies:
    1
    Views:
    284
    Diez B. Roggisch
    Nov 9, 2005
  5. Brian Blais
    Replies:
    1
    Views:
    365
    Bruno Desthuilliers
    Jun 27, 2006
Loading...

Share This Page