Hi,
When defining my new type in C++, I have set the "tp_getattro" field to
point to a function (say "mytype_getattro").
This function determine dynamically if the requested attribute exists or
not, and if it does, return its value.
So, it works well for attributes/properties.
But, "mytype_getattro" is also called when, in Python, I do a function
call 'myobject.myfct(...)'. It allows me to determine
dynamically if the requested function "myfct" is available or not. But
if it does, I need to return a callable object to allow
python to process the call.
I succeeded to make Python to call a function of mine using a
PyMethodDef array and using the Py_FindMethod function
to get a callable PyObject*.
But it is not a good solution for at least two reasons :
1 - Py_FindMethod is not supported in Python 3.0
2 - I need to declare each possible function in the array (which is
impossible has I need to determine them at runtime)
What I have tried to do is to declare only one "dipatch" function in the
array which will call the right C++ function.
This way, in "mytype_getattro" I do a "return Py_FindMethod(myarray,
self, "dispatch")". Thus, python call my
dispatching method. The problem is that I need to attach additionnal
values to the function (like the name of the function
which must be called in reality). In Lua, I used the closure mechanism,
but I did not success with Python closures.
Any hints ?
Is closure the right approach (If so, how I should use it ?)? Do I need
to set another field when defining my type ?
Regards,
Jérémie Delaitre
Jérémie Delaitre a écrit :
It works ! Thanks a lot.
Jérémie
Alex Mohr a écrit :
Override the python special methods __getattr__ or __getattribute__.
http://docs.python.org/reference/datamodel.html#attribute-access
Alex
Jérémie Delaitre wrote:
Hello,
I have a C++ class that use a custom property system.
This property system allows me to add and remove properties at runtime.
I want to expose this class into Python.
The problem is: I want "a dynamic resolution" of the properties in
Python.
For example:
# import my python extension
import myModule
# get an instance of my C++ class
t = myModule.newInstance()
# access the property 'myProperty'
print(t.myProperty)
When I access the property, I want python to call a "callback" of
mine (written
in the C++ extension) which will check if the requested property
exists, and
if it does, return the corresponding value, otherwise generate an
exception.
By looking at the python C API, I did not find a way to achieve
this. It seems
that declaring a new type requires to have static arrays for members
and methods
(yes, my property system allows me to declare new methods too, so I
want to expose
them with a similar system).
Any idea how I could resolve this (with Python3.0) ?
Regards,
Jérémie Delaitre
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig