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


Groups > comp.lang.python > #99077

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

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Why doesn't this method have access to its "self" argument?
Date 2015-11-19 19:00 +0100
Organization None
Message-ID <mailman.474.1447956061.16136.python-list@python.org> (permalink)
References <db6apoFgvjlU1@mid.individual.net>

Show all headers | View raw


Robert Latest via Python-list wrote:

> I found a workaround using a wrapper method which calls a C function,
> passing the instance as a separate argument. It works, and I cannot see
> any disadvantage. It's just not as elegant as I'd like it to be, and I
> don't understand WHY the C "method" doesn't receive a pointer to the
> Python instance. Maybe somebody can clarify.

I don't know much about the C-implementation side, but functions written in 
Python are also descriptors. Given

def f(self): pass

class A(object):
    m = f
    c = abs
    v = 42

a = A()

a seemingly simple attribute access

m = a.m      # m is now a bound method

invokes f.__get__(a, A) under the hood while

m = a.c
assert m is abs

just returns the function (abs in the example) the same way it returns any 
other value:

v = a.v
assert v == 42

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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