Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import class from python module (boost.python)?

I have a python function:

def log(text):
    print text

saved in Callbacks.py file. Now I want to import it to c++ function and execute. This works fine:

py_fun = import("Callbacks");
py_fun.attr("log")(text);

But I would like to make log function part of a class:

class Logger:    
    def __init__(self):
        self.last_read = -1

    def log(self, text):
        print text

How can I import it to C++ and create an instance of Logger?

like image 446
Pawel Avatar asked Apr 19 '26 11:04

Pawel


1 Answers

Exactly the way you'd think:

py::object mod = py::import("Callbacks");
py::object logger = mod.attr("Logger")();
like image 157
Barry Avatar answered Apr 20 '26 23:04

Barry