Re: python 3 problem: how to convert an extension method into a classMethod

Discussion in 'Python' started by Ethan Furman, Feb 26, 2013.

  1. Ethan Furman

    Ethan Furman Guest

    On 02/26/2013 09:21 AM, Robin Becker wrote:
    > In python 2 I was able to improve speed of reportlab using a C extension to optimize some heavily used methods.
    >
    > so I was able to do this
    >
    >
    > class A:
    > .....
    > def method(self,...):
    > ....
    >
    >
    > try:
    > from extension import c_method
    > import new
    > A.method = new.instancemethod(c_method,None,A)
    > except:
    > pass
    >
    > and if the try succeeds our method is bound as a class method ie is unbound and works fine when I call it.
    >
    > In python 3 this doesn't seem to work at all. In fact the new module is gone. The types.MethodType stuff doesn't seem to
    > work.
    >
    > Is there a way in Python 3.3 to make this happen? This particular method is short, but is called many times so adding
    > python wrapping layers is not a good way forward.


    Dumb question, but have you tried just assigning it? In Py3 methods are just normal functions...

    8<----------------------
    class A():
    pass

    A.method = c_method
    8<----------------------

    --
    ~Ethan~
     
    Ethan Furman, Feb 26, 2013
    #1
    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. Robin Becker
    Replies:
    1
    Views:
    134
    Steven D'Aprano
    Feb 27, 2013
  2. Dave Angel
    Replies:
    0
    Views:
    126
    Dave Angel
    Feb 26, 2013
  3. Peter Otten
    Replies:
    0
    Views:
    125
    Peter Otten
    Feb 26, 2013
  4. Mark Lawrence
    Replies:
    0
    Views:
    132
    Mark Lawrence
    Feb 26, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    134
    Peter Otten
    Feb 26, 2013
Loading...

Share This Page