self in extension method

Alex Martelli aleax at aleax.it
Fri May 3 12:25:39 EDT 2002


David Morgenthaler wrote:

> My question concerns getting access to self (an instance object) in a
> C extension. I'm trying to implement an extension method, but when I
> call the method, self always evals to Py_None. Here's the gist of what

You have to "wrap up" the 'builtin' (c-implemented) function before
adding it to the (Python-implemented) class.

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/54352 shows
how to implement a Python class entirely in C, but you can use just
the wrap-functions-appropriately part of that.

Or, Python-side, instead of:

A.methodofinterest = methodofinterest_

something like

A.methodofinterest = lambda *a, **k: methodofinterest_(*a, **k)

should also be roughly equivalent, as should the named equivalent:

def methodofinterest(*a, **k):
    return methodofinterest_(*a, **k)
A.methodofinterest = methodofinterest

You do need nested scopes for this, unless you use a global variable
for methodofinterest_ of course.


Alex




More information about the Python-list mailing list