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


Groups > comp.lang.python > #99128

Re: Why doesn't this method have access to its "self" argument?

From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: Why doesn't this method have access to its "self" argument?
Date 2015-11-20 08:18 +0100
Message-ID <mailman.507.1448003893.16136.python-list@python.org> (permalink)
References <db6apoFgvjlU1@mid.individual.net>

Show all headers | View raw


Robert Latest via Python-list <python-list@python.org> 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)

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


Thread

Why doesn't this method have access to its "self" argument? Robert Latest <boblatest@yahoo.com> - 2015-11-19 16:13 +0000
  Re: Why doesn't this method have access to its "self" argument? Chris Angelico <rosuav@gmail.com> - 2015-11-20 04:34 +1100
  Re: Why doesn't this method have access to its "self" argument? Peter Otten <__peter__@web.de> - 2015-11-19 19:00 +0100
  Re: Why doesn't this method have access to its "self" argument? dieter <dieter@handshake.de> - 2015-11-20 08:18 +0100

csiph-web