1

I am using pybind11 to call a python built-in function like range in c++ code. But I only found way to call a function in module like this:

py::object os = py::module::import("os");
py::object makedirs = os.attr("makedirs");
makedirs("/tmp/path/to/somewhere");

But a python built-in function like range needn't import any modules, so how can I use pybind11 to call range in c++ code?

2 Answers 2

1

You could fetch range from the globals dict.

Sign up to request clarification or add additional context in comments.

Comments

0

You can also import the builtins module which contains all the built-in python functions.

In your case it would be something like:

py::object builtins = py::module_::import("builtins");
py::object range = builtins.attr("range");
range(0, 10);

Comments

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.