3

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?

1 Answer 1

3

Exactly the way you'd think:

py::object mod = py::import("Callbacks");
py::object logger = mod.attr("Logger")();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It works. i knew I was missing something. And I was missing parentheses.

Your Answer

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.