3

我希望为一堆 c++ 类实现 python 包装器。在 pxd 的某个地方,我有:

cdef cppclass FooImpl1:
    FooImpl1()
    int foo()

cdef cppclass FooImpl2
    FooImpl2()
    int foo()

我想知道我是否可以在 pyx python 包装器中写这样的东西:

ctypedef fused FooImpl:
    FooImpl1*
    FooImpl2*

cdef class Foo:
    cdef FooImpl impl
    def __cinit__(self, int selector):
        if selector == 1:
            self.impl = new FooImpl1()
        else:
            self.impl = new FooImpl2()

    def func(self):
        # depending on the object stored in impl FooImpl2::foo or FooImpl1::foo
        # will be called
        return self.impl.foo()

有没有办法完成预期的行为?FooImpl1 和 FooImpl2 不共享抽象接口,它们是类的模板特化。

4

1 回答 1

4

As of this version (0.20), Cython doesn't support fused types in classes, only in function parameters and variables. Here are the docs.

于 2014-03-24T00:34:55.727 回答