views:

101

answers:

1

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.

A: 

__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.

Matthew Flaschen
`__getattr__` and `__setattr__` seem to be defining attributes with a subscript. I just need (unless I am misunderstanding) something like a module-level global variable but with a getter and a setter on C++ side.
Alex B