Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39976

python 3 problem: how to convert an extension method into a class Method

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'heavily': 0.04; 'that?': 0.05; 'except:': 0.07; 'layers': 0.07; 'try:': 0.07; 'python': 0.09; 'happen?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'subject:method': 0.09; 'def': 0.10; 'subject:python': 0.11; 'extension': 0.13; '(another': 0.16; 'bind': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject: \n ': 0.16; 'subject:class': 0.16; 'unbound': 0.16; 'instance': 0.17; 'module': 0.19; 'import': 0.21; 'subject:problem': 0.22; 'work.': 0.23; 'external': 0.24; 'pass': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'all.': 0.28; 'fine': 0.28; 'methods.': 0.29; 'class': 0.29; 'stuff': 0.30; 'sense': 0.31; 'implement': 0.32; 'could': 0.32; 'to:addr:python- list': 0.33; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'too': 0.36; 'bad': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'kind': 0.61; 'times': 0.63; 'great': 0.64; 'our': 0.65; 'skip:n 30': 0.69; 'received:109': 0.74; '.....': 0.75; 'forward.': 0.84; 'penalty': 0.84; 'presumably': 0.84; 'victory': 0.84; 'imagine': 0.96
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Robin Becker <robin@reportlab.com>
Subject python 3 problem: how to convert an extension method into a class Method
Date Tue, 26 Feb 2013 17:21:16 +0000
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 109.174.168.73
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130215 Thunderbird/17.0.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2558.1361899289.2939.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361899289 news.xs4all.nl 6897 [2001:888:2000:d::a6]:47666
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39976

Show key headers only | View raw


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.

Alternatively could it make sense to implement an accelerated basetype that just 
contains the accelerated methods of class A. I could then imagine doing 
something like

try:
     from extension import class c_baseA as baseA
except:
     class baseA:
        def method(....)

class A(baseA):
     .....

presumably I then get some kind of penalty for the base class lookup, but how 
bad is that?
-- 
Robin Becker

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

python 3 problem: how to convert an extension method into a class Method Robin Becker <robin@reportlab.com> - 2013-02-26 17:21 +0000
  Re: python 3 problem: how to convert an extension method into a class Method Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-27 04:46 +0000

csiph-web