Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a property to a module in boost::python?

You can add a property to a class using a getter and a setter (in a simplistic case):

class<X>("X")
    .add_property("foo", &X::get_foo, &X::set_foo);

So then you can use it from python like this:

>>> x = mymodule.X()
>>> x.foo = 'aaa'
>>> x.foo
'aaa'

But how to add a property to a module itself (not a class)?

There is

scope().attr("globalAttr") = ??? something ???

and

def("globalAttr", ??? something ???);

I can add global functions and objects of my class using the above two ways, but can't seem to add properties the same way as in classes.

like image 405
Alex B Avatar asked Apr 29 '10 05:04

Alex B


1 Answers

__getattr__ and __setattr__ aren't called on modules, so you can't do this in ordinary Python without hacks (like storing a class in the module dictionary). Given that, it's very unlikely there's an elegant way to do it in Boost Python either.

like image 61
Matthew Flaschen Avatar answered Oct 23 '22 14:10

Matthew Flaschen