[C++-sig] Dynamic resolution of members and methods

Alex Mohr amohr at pixar.com
Wed Dec 10 18:55:48 CET 2008


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 at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig



More information about the Cplusplus-sig mailing list