2

How do you use basic ("default"/"built-in"; don't need to be imported) python methods in pybind11?

Lists, dictionaries, and some others do have built-in compatibility, but the method I am looking specifically for (open) is not included via an import. I know that a way around it would be to create a python file with a method wrapping "open" and then calling it as you would any imported method, but I would prefer to do it directly in C++ (using pybind) if possible as otherwise that semi-defeats the purpose.

Any assistance/advice would be greatly appreciated.

1

1 Answer 1

4

You are wrong. First, built-in names are importable from builtins module (in Python 3):

py::object open = py::module::import("builtins").attr("open");

Second, open also lives in io module so you can also use the following line that is equivalent to the line above:

py::object open = py::module::import("io").attr("open");

This is for Python 3, but the last line works for Python 2.7 too.

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

2 Comments

The builtin open has a different signature from io.open on Python 2, though.
Oh, okay. Thank you! :D

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.