简体   繁体   中英

In boost.python; how do I expose a class that is contained in another class (via composition)?

I'd like to do something really simple with boost::python. I can find documentation for class member functions, and documention for inherited classes but nowhere can I find the syntax for exposing class hierarchies created via composition.

So I have some C++ code that goes something like this:

struct A{
    double x,y;
};

struct B{
    A foo;
    double z;
};

And I want to expose both classes so that in python I can write something like:

spam = A()
spam.x=1
spam.y=2

eggs = B()
eggs.foo=spam
eggs.z = 33
Print eggs.foo.y

Surely that's possible? But I can't figure it out.

Many thanks!

EDIT:

False alarm... it's done automatically; if you use the following export code to export each individually:

class_<A>("A")
   .def_readwrite("x",&A::x)
   .def_readwrite("y",&A::y)
;

class_<B>("B")
  .def_readwrite("z",&B::z)
  .def_readwrite("foo",&B::foo)
;

What threw me is that you have to instantiate the class under python before the full list of submethods becomes visible with dir() ie, the following produce different results, and you must use the second type to get a full member listing:

dir(B.foo)
dir(B().foo) 

Evidently some python technicalities going on here which I don't yet understand... any further clarification welcome.

Documentation for dir says:

If the object is a type or class object, the list contains the names of its attributes, and recursively of the attributes of its bases.

In your example, your class member are exported as instance attributes and not class attributes which is what you want when exporting non static class member. This is why you need to instantiate the class in python in order for dir to return the attributes because the attributes do not exist until the init method is called.

When declaring class attributes, they will show when calling dir on type because class attributes right after class definition:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...     name = "blah"
...     def __init__(self):
...         self.second_name = "blah2"
...
>>> dir(Foo)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribut
e__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_e
x__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_
_weakref__', 'name']
>>> f = Foo()
>>> f
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribut
e__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_e
x__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_
_weakref__', 'name', 'second_name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM