在boost.python中;如何公开包含在另一个类中的类(通过组合)?

发布于 2024-11-03 04:27:43 字数 835 浏览 1 评论 0原文

我想用 boost::python 做一些非常简单的事情。我可以找到类成员函数的文档以及继承类的文档,但我找不到用于公开通过组合创建的类层次结构的语法。

所以我有一些 C++ 代码,如下所示:

struct A{
    double x,y;
};

struct B{
    A foo;
    double z;
};

我想公开这两个类,以便在 python 中我可以编写如下内容:

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

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

当然可以吗?但我无法弄清楚。

非常感谢!

编辑:

误报......它是自动完成的;如果您使用以下导出代码单独导出每个:

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)
;

让我困惑的是,您必须在子方法的完整列表通过 dir() 可见之前在 python 下实例化该类,即,以下会产生不同的结果,并且您必须使用第二种类型以获得完整的成员列表:

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

显然这里有一些我还不明白的Python技术细节......欢迎任何进一步的澄清。

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() i.e, 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2024-11-10 04:27:43

dir 的文档说:

如果对象是类型或类对象,则列表包含其属性的名称以及其基属性的递归名称。

在您的示例中,您的类成员被导出为实例属性,而不是类属性,这是导出非静态类成员时您想要的。这就是为什么您需要在 python 中实例化该类,以便 dir 返回属性,因为在调用 init 方法之前,属性并不存在。

声明类属性时,它们将在类型上调用 dir 时显示,因为类属性紧随类定义之后:

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']

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']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文