Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: python 3 problem: how to convert an extension method into a classMethod

Reply
Thread Tools

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

 
 
Mark Lawrence
Guest
Posts: n/a
 
      02-26-2013
On 26/02/2013 18:38, Peter Otten wrote:
> 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.
>>
>> If the above cannot be made to work (another great victory for Python 3)
>> then is there a way to bind an external method to the instance without
>> incurring too much overhead.

>
> Hm, according to my random measurement your clever approach incurs more
> overhead than the straight-forward way that continues to work in Python 3:
>
> $ python -m timeit -s 'from new import instancemethod
>> from math import sqrt
>> class A(int): pass
>> A.m = instancemethod(sqrt, None, A)
>> a = A(42)
>> ' 'a.m()'

> 1000000 loops, best of 3: 0.5 usec per loop
> $ python -m timeit -s 'from math import sqrt
>> class A(int):
>> def m(self):
>> return sqrt(self)
>> a = A(42)
>> ' 'a.m()'

> 1000000 loops, best of 3: 0.473 usec per loop
>
>


c:\Users\Mark\MyPython>python
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:4 [MSC v.1600 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import new

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'new'

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python 3 problem: how to convert an extension method into a classMethod Robin Becker Python 1 02-27-2013 04:46 AM
Re: python 3 problem: how to convert an extension method into a classMethod Peter Otten Python 0 02-26-2013 08:26 PM
Re: python 3 problem: how to convert an extension method into a classMethod Ethan Furman Python 0 02-26-2013 08:19 PM
Re: python 3 problem: how to convert an extension method into a classMethod Peter Otten Python 0 02-26-2013 06:38 PM
Re: python 3 problem: how to convert an extension method into a classMethod Dave Angel Python 0 02-26-2013 05:52 PM



Advertisments