如何通过Swig为Python类创建别名?

我已经创建了一个C库,并成功使用swig使其可以通过
python访问.模板中大量使用模板,每个模板类型都通过swig映射到自己的python类,如下所示:

%template(Imageint) Image<int>;
%template(Imagedouble) Image<double>;

但是,我真的希望有一个python使用的’默认’模板

a = Image("filename")

实例化Image< double>无需始终输入

a = Imagedouble("filename")

Swig文档说明:

The %template directive should not be
used to wrap the same template
instantiation more than once in the
same scope. This will generate an
error. This error is caused because
the template expansion results in two
identical classes with the same name.
This generates a symbol table
conflict. Besides, it probably more
efficient to only wrap a specific
instantiation only once in order to
reduce the potential for code bloat.

所以为了避免符号表冲突,我试过了

%rename(Image) Image<double>;
%template(Imageint) Image<int>;
%template(Imagedouble) Image<double>;

在接口文件中.然而,swig然后抱怨Image被重新定义.

制作别名的最佳方法是Image和Imagedouble都引用C Image< double>?非常感谢您提供的任何帮助.

-Josh

最佳答案 如果这有助于将来的某个人,那么执行上述操作的方法是将以下内容添加到接口文件中:

%pythoncode %{
Image = Imagedouble
%}

我没有意识到有一种方法可以在接口文件中编写标准的python代码.

点赞