Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: python 3 problem: how to convert an extension method into a classMethod (http://www.velocityreviews.com/forums/t958111-re-python-3-problem-how-to-convert-an-extension-method-into-a-classmethod.html)

Peter Otten 02-26-2013 08:54 PM

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

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


The problem is that functions implemented in C don't support the descriptor
protocol (they don't have a __get__() method). So

>>> from math import sqrt
>>> class A(int):

.... pass
....
>>> A.c = sqrt
>>> A.py = lambda self: sqrt(self)
>>> a = A(42)
>>> a.py()

6.48074069840786
>>> a.c()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sqrt() takes exactly one argument (0 given)




All times are GMT. The time now is 07:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.