lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

Peter Loveday wrote:
> Does anyone know if there is a way in Python to create some sort of 
> function closures from the tp_getattro function?

1. Use PyCFunction_NewEx() to create a bound method. Pass in a
PyObject holding whatever you want the called C function to
receive. You need a different PyMethodDef for each C function.
And you may need a wrapper function to extract the fields from
the passed object. And if there's no suitable predefined object
type, you'll have to create your own, too ...

Ok, so better try this:

2. Create your own Python type, holding a function pointer and
any additional fields you need. Return newly created instances
from your dispatcher, filling in the fields as needed. The type
needs to have a tp_call handler which calls the function pointer,
passing the whole object or individual fields. Or just put the
whole code into tp_call if it's short.

The second approach has the advantage that you only need to
create one PyObject per dispatch. Calling it is faster, too.
You may want to take a peek at methodobject.*.

[Ok, now try asking a Lua question on python-dev. ;-)]

Bye,
     Mike