我希望为一堆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不共享抽象接口,它们是类的模板特化。