Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7050343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:07:55+00:00 2026-05-28T03:07:55+00:00

I need to add a new method to my swig template class, for example:

  • 0

I need to add a new method to my swig template class, for example:

I am declaring a template class in myswig.i as follows:

%template(DoubleVector) vector<double>;

this will generate a class named “DoubleVector” in the generated .py file with some generated methods. lets suppose they are func1(), func2() and func3().
These are generated functions and i have no control over them.
Now, if I want to add a new method called “func4()” to this class(DoubleVector), how may I do it? Is it possible?

I know of a identifier called %pythoncode but I could not use it to define a new function in this template class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:07:55+00:00Added an answer on May 28, 2026 at 3:07 am

    Given an interface file like:

    %module test
    
    %{
    #include <vector>
    %}
    
    %include "std_vector.i"
    %template(DoubleVector) std::vector<double>;
    

    The easiest way to add more functionality to DoubleVector is to write it in C++, in the SWIG interface file using %extend:

    %extend std::vector<double> {
      void bar() {
        // don't for get to #include <iostream> where you include vector:
        std::cout << "Hello from bar" << std::endl;       
      }
    }
    

    this has the advantage that it would work for any language you target with SWIG, not just Python.

    You can also do it using %pythoncode and an unbound function:

    %pythoncode %{
    def foo (self):
            print "Hello from foo"
    
    DoubleVector.foo = foo
    %}
    

    Example running this:

    Python 2.6.7 (r267:88850, Aug 11 2011, 12:16:10) 
    [GCC 4.6.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import test
    >>> d = test.DoubleVector()
    >>> d.foo()
    Hello from foo
    >>> d.bar()
    Hello from bar
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i add a new method to NSString class. I need to create
I need to add default value column in SqlServer Compact....i am new to this
Or, in other words, what is wrong with something like - new Method[] {Vector.add(),
I need add a new user group for mediawiki. The new group has more
I need to add a new column to a MS SQL 2005 database with
I need to add a new SELECT statement whose search condition include something like
After inserting new html to DOM,I need to add some listeners to it. But
I need to add new records to my SQL database using C# and php.
I need to add 'ADD NEW ROW' cell to the table. And I add
Could you please tell how to add a new method to Dojo? It works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.