1

I have created a class Hello.h like below:

#include <string>
class Hello {
public:
    Hello() {};
    std::string greet() {
         cout<<"HelloWorld"<<endl;
    };
};

and I had using SWIG exported this class as a python module and get two files: Hello.py and _Hello.so, I can import this module in python env and call greet() method successfully as below:

import Hello
a = Hello.Hello() 
a.greet()

My problems is that I had created some Hello object in C++, say objectHello , and I want to call objectHello's method in python through the exported module, below is what I would like to do:

 import Hello
 a = Hello.Hello() // My idea is that how to change this line when "a" is created in C++, how to pass it to python and thus I can call it's method in python env
 a.greet()

this case is very simple, in real case, there may be many parameters for my C++ methods. How can I do to achieve that, or convince me if this is a wrong direction, can someone give me some tips on this, thanks in advance.

BruceSun
  • 144
  • 9
  • I think you should do `a = Hello()` as you don't normally call constructors in Python (and in C++) directly. – ForceBru Jul 25 '16 at 18:38
  • @ForceBru I could use exported method successfully, thanks for your concern, but this is not the main point of my question. – BruceSun Jul 25 '16 at 18:43
  • 1
    There have been a great many posts to SO over these many years which describe how to construct a Python "binding." http://stackoverflow.com/questions/135834/python-swig-vs-ctypes#135966 received many excellent answers. Unfortunately I think that you have stepped outside of the boundaries of "what SWIG knows how to do." First, you want to instantiate a C++ object and bind it to a Python variable. Then, you want to provide interfaces to that object's methods. Frankly, I think that's going too far. I'd cook-up a "SWIG-friendly library" that owns object instances and manages them. – Mike Robinson Jul 25 '16 at 19:01
  • @MikeRobinson the user case for above is that I had a C++ canvas library and also I exported it to python module, I first write an application using the C++ canvas library, then I would like to manipulate the application using python through the exported python module. How to share C++ object to python seem not easy. – BruceSun Jul 26 '16 at 00:29
  • No, it will not be easy at all. Python uses a very different garbage-collection and reference counting strategy. It would therefore be difficult to manage having a handle to the object, and you'll have to build an interface to facilitate every method-call. Hence my suggestion to create a Python-friendly "wrapper" library that would actually own the object instances (perhaps using some sort of "ID" number or string that Python could be told about) and performs all the calls. The details could then be buried in a Python class-definition. But, it will be messy. Very messy. – Mike Robinson Jul 26 '16 at 12:55
  • @Mike Robinson there is some hack about SWIG to achieve this, maybe we can have a try http://www.cnblogs.com/xiao-liang/p/3569277.html – BruceSun Jul 26 '16 at 23:20
  • *"Yuck!"* Mebbe so ... mebbe so ... – Mike Robinson Jul 26 '16 at 23:41
  • Are you just looking for global variables in your C++? That's pretty easy to do in SWIG. – Flexo Aug 21 '16 at 19:12

0 Answers0