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
  • 35,554
  • 7
  • 89
  • 134
  • 1
    I think it's a duplicate:https://stackoverflow.com/q/3286448/10077 – Fred Larson Feb 15 '18 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. – Zakharov Aleksey Feb 15 '18 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. – Fred Larson Feb 15 '18 at 15:12
  • 2
    Maybe this https://stackoverflow.com/q/39813301/1741542 is better suited, because it creates an object and then uses it. – Olaf Dietsche Feb 15 '18 at 16:12
  • @OlafDietsche thank you very much for this link. It helps a lot :) – Zakharov Aleksey Feb 16 '18 at 09:50
  • I found the answer on russian stackoverflow: [link to answer](https://ru.stackoverflow.com/questions/785574/%D0%97%D0%B0%D0%BF%D1%83%D1%81%D1%82%D0%B8%D1%82%D1%8C-python-%D0%BA%D0%BB%D0%B0%D1%81%D1%81-%D0%B8%D0%B7-%D0%BA%D0%BE%D0%B4%D0%B0-c/785927#785927) – Zakharov Aleksey Feb 16 '18 at 09:53
  • I can read the code, although I must skip the Russian part ;-) – Olaf Dietsche Feb 16 '18 at 09:57

0 Answers0