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 7717767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:59:10+00:00 2026-06-01T02:59:10+00:00

What is the SWIG equivalent to storing a copy of an arbitrary python object?

  • 0

What is the SWIG equivalent to storing a copy of an arbitrary python object?

I’m pretty sure what I’m asking is possible because it would work with boost::python (see below), but I can’t see a way to do this using SWIG.

#include <boost/python.hpp>

using namespace boost::python;

class MyClass
{
public:
    // other operations
    object get_info() { return info_; }
    void set_info(object info) { info_ = info; }

private:
    object info_;
};

BOOST_PYTHON_MODULE( mymodule )
{
    class_<MyClass>("MyClass")
        .def("get_info", &MyClass::get_info )
        .def("set_info", &MyClass::set_info )
    ;
}
  • 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-06-01T02:59:11+00:00Added an answer on June 1, 2026 at 2:59 am

    The simplest example is:

    %module test
    
    %inline %{
    class MyClass
    {
    public:
        // other operations
        PyObject *get_info() { return info_; }
        void set_info(PyObject *info) { info_ = info; }
    
    private:
        PyObject *info_;
    };
    %}
    

    e.g.:

    swig -python -Wall -c++ test.i
    g++ -Wall -Wextra test_wrap.cxx -I/usr/include/python2.6 -o _test.so -shared
    python
    Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import test
    >>> foo = test.MyClass()
    >>> str = "hi"
    >>> foo.set_info(str)
    >>> print foo.get_info()
    hi
    >>>
    

    But note that in this case the ownership of the object has not been changed. If I’d done foo.set_info("hi") instead then it would have been released because no references were retained by the time it called foo.get_info().

    You can fix that by adding a call to:

    Py_INCREF(info);
    

    inside set_info(), but then you’ll need a corresponding DECREF in the destructor, or if set_info() is called when a reference is already held, or for copy construction or assignment. (Or some nice RAII type to do all that for you…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

IB = Interactive Brokers There seem to be two main choices SWIG Boost.Python +
Would it be possible using Lua and SWIG and say an IInterface class, to
when generating python wrappers with swig the python wrapper classes in the generated python
I use Python with C (with SWIG), where the main module in Python processes
I have a python module that imports a module generated with swig. When I
I'm using SWIG to wrap my C++ code into Python. But the conversion of
I'm using SWIG 2.0 to create a Python wrapper for a C++ library. One
Using SWIG to generate a Python interface to a C++ application, is there any
Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question
I just read the following section of the SWIG and Python tutorial: http://www.swig.org/Doc1.1/HTML/Python.html#n11 I

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.