Path: csiph.com!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=python.org; s=200901; t=1361912894; bh=TyZeW0IcsLFXdtJZbFbVHGVURcDDoBQf2qwbamQdIsg=; h=To:From:Subject:Date:Message-ID:References:Mime-Version: Content-Type:Content-Transfer-Encoding:In-Reply-To; b=qHRJdCk85PtnMSyAIZN0Fpwbgq6/70Fy4//wsYroyDnRRVbNgIExqBpzB0G4nwboT +ERXUFNUeoe8pc8i5BCU+wW2htk2CrJxecFQoc+CbN5DJe8olu6fgYcGcxfpbz2Syr xtyD65fwd1eIbPOQ/F4mJAVm+olY04WIchuTlRlc= X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'argument': 0.04; 'method.': 0.05; 'builtin': 0.07; 'python': 0.09; 'assigning': 0.09; 'descriptor': 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; 'typeerror:': 0.09; 'subject:python': 0.11; '_testcapi': 0.16; 'a():': 0.16; 'from:name:christian heimes': 0.16; 'given)': 0.16; 'py3': 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; 'from:addr:python.org': 0.17; '>>>': 0.18; 'feb': 0.19; 'import': 0.21; '"",': 0.22; 'object.': 0.22; 'subject:problem': 0.22; 'skip:_ 20': 0.22; 'linux': 0.24; 'pass': 0.25; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'implemented': 0.27; "doesn't": 0.28; 'header:X -Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'wrap': 0.29; 'class': 0.29; 'code': 0.31; 'implement': 0.32; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'too.': 0.35; 'something': 0.35; 'received:org': 0.36; 'except': 0.36; 'but': 0.36; 'method': 0.36; 'skip:p 20': 0.36; 'rather': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'easily': 0.39; 'header:Received:5': 0.40; 'more': 0.63; '3000': 0.71; 'acts': 0.71; 'dumb': 0.84; 'ethan': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Christian Heimes Subject: Re: python 3 problem: how to convert an extension method into a class Method Date: Tue, 26 Feb 2013 22:08:02 +0100 References: <512CEF0C.3020906@chamonix.reportlab.co.uk> <512D18C2.5000802@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: f048033189.adsl.alicedsl.de User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: <512D18C2.5000802@stoneleaf.us> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361912896 news.xs4all.nl 6840 [2001:888:2000:d::a6]:57632 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:40007 Am 26.02.2013 21:19, schrieb Ethan Furman: > 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<---------------------- That doesn't work with builtin functions because they don't implement the descriptor protocol: Python 3.3.0 (v3.3.0:bd8afb90ebf2, Feb 8 2013, 00:38:29) [GCC 4.7.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> class Example: ... id = id ... >>> Example().id() Traceback (most recent call last): File "", line 1, in TypeError: id() takes exactly one argument (0 given) You can use PyInstanceMethod_Type to wrap a builtin method in something that acts like a normal method. I implemented the type for Python 3000 when I removed the unbound method object. It's not available to pure Python code except for testing: >>> import _testcapi >>> class Example: ... id = _testcapi.instancemethod(id) ... >>> Example().id() 140525206026320 The C code is rather simple and small. You can easily re-implement in Python, too.