Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Why doesn't this method have access to its "self" argument? Date: Fri, 20 Nov 2015 08:18:02 +0100 Lines: 31 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de ybyT44LZWoxQby3QQwdJCAUxw2jqGbXPrr1H9ohOpNZw== Cancel-Lock: sha1:J2zhpUOIHKcqyq57CWcuBbWRTNE= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '(so': 0.07; '*args,': 0.07; 'wrapper': 0.07; 'descriptor': 0.09; "object's": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'subject:method': 0.09; 'python': 0.10; '(at': 0.13; 'def': 0.13; '(but': 0.15; 'class)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:access': 0.16; '(in': 0.18; 'trying': 0.22; 'import': 0.24; 'implemented': 0.24; 'header :User-Agent:1': 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:_ 20': 0.26; 'skip:m 30': 0.27; 'least': 0.27; 'errors.': 0.27; 'function': 0.28; 'interface': 0.29; 'be:': 0.29; "i'm": 0.30; 'implement': 0.32; 'class': 0.33; 'skip:_ 30': 0.33; 'advice': 0.35; 'easiest': 0.35; 'robert': 0.35; 'problem.': 0.35; 'but': 0.36; 'subject:" ': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'easy': 0.60; 'confirm': 0.62; 'latest': 0.64; 'python-list': 0.66; 'received:217': 0.66; 'subject:have': 0.80; '**kw)': 0.84; '**kw):': 0.84; 'subject:its': 0.84; 'subject:self': 0.84; 'subject:this': 0.85; 'severe': 0.91; 'good,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pd9e09e1b.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99128 Robert Latest via Python-list writes: > I'm still trying to find a simple way to implement a method (but not the > full class) in C. Suggestions I have recived so far are good, but seem to be > over the top for such a (seemingly) simple problem. The C interface of Python is far from simple - and it is very easy to make severe and difficult to analyse errors. Therefore, I confirm an advice you already got: use "cython" (which drastically reduces the danger of errors). A "method" in Python is just a function which happens to get automatically on call an additional first arguemnt: the object's reference. Apparently, this automatism does not work for C implemented functions (at least in Python 2). You would need to use a (so called) "descriptor" wrapper to do this explicitely. The easiest way (in my view), however would be: import _my_c_implementation class C(object): def my_method(self, *args, **kw): return _my_c_implementation.my_method(self, *args, **kw) With a descriptor "method" (sketched above), this would become: class C(object): my_method = method(_my_c_implementation.my_method)