How do I create an alias to a Python class through Swig?

I've created a C++ library and have successfully used swig to make it accessible through python. Templating is used heavily in the library and each template type is mapped to its own python class by swig like so:

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

However, I would really like to have a 'default' template used by python such that

a = Image("filename")

instantiates Image<double> without having to always type

a = Imagedouble("filename")

Swig documentation states:

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.

So to avoid the symbol table conflict, I tried

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

in the interface file. However, swig then complains about Image being redefined.

What is the best way to make an alias such that Image and Imagedouble both refer to C++ Image<double>? Thank you very much for any help you can provide.



In case this helps someone in the future, the way to do what is describe above is to add the following to the interface file:

%pythoncode %{
Image = Imagedouble

I did not realize there was a way to write standard python code in the interface file.

Need Your Help

How to attach OnClickListener to a button in a custom preference layout?

android layout preferences

I have a preference screen that is populated with items from a database. I have this working by creating my own PreferenceActivity. In the activity I create DialogPreference items and add them to...

Getting Primary key values (auto number ) VB ms-access

I have a database on Access and I want to insert into 2 tables

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.