5

For example let us consider python class with the following structure in filemult.py.

mult.py

class Mult: 
 def __init__(self, first, second):
 self.a = first 
 self.b = second 
 def multiply(self):
 c = self.a*self.b
 print 'The result of', self.a, 'x', self.b, ':', c
 return c

How could we call multiply method from class above in C++ Code? (But without PyRun_SimpleString() function)

main.cpp

#include<python2.7/Python.h>
#include <boost/python.hpp>
int main()
{
 Py_Initialize();
 int a {10};
 int b {5};
 ... CODE HERE (a*b using python function)
 Py_Finalize();
}

I was looking a solution in Cython and boost.python but could not find it.

Additional links:

iBug
37.6k8 gold badges101 silver badges156 bronze badges
asked Feb 15, 2018 at 14:40
7
  • 1
    I think it's a duplicate:stackoverflow.com/q/3286448/10077 Commented Feb 15, 2018 at 14:45
  • @FredLarson, thank you for your link (It is very helpful). But i didn't find how to get access (what function) to class (which in python module) and after that call some method of this class. If you could give a link to simple example, this would be great. Commented Feb 15, 2018 at 15:01
  • 1
    I think you'd just import it using something like PyObject* mult_module = PyImport_ImportModule("mult");. There are examples in the answers to the linked question. Commented Feb 15, 2018 at 15:12
  • 2
    Maybe this stackoverflow.com/q/39813301/1741542 is better suited, because it creates an object and then uses it. Commented Feb 15, 2018 at 16:12
  • @OlafDietsche thank you very much for this link. It helps a lot :) Commented Feb 16, 2018 at 9:50

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.