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

Peter Otten __peter__ at web.de
Thu Nov 19 13:00:15 EST 2015


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





More information about the Python-list mailing list