Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 1 | Utilities |
| 2 | ######### |
| 3 | |
| 4 | Using Python's print function in C++ |
| 5 | ==================================== |
| 6 | |
| 7 | The usual way to write output in C++ is using ``std::cout`` while in Python one |
| 8 | would use ``print``. Since these methods use different buffers, mixing them can |
| 9 | lead to output order issues. To resolve this, pybind11 modules can use the |
| 10 | :func:`py::print` function which writes to Python's ``sys.stdout`` for consistency. |
| 11 | |
| 12 | Python's ``print`` function is replicated in the C++ API including optional |
| 13 | keyword arguments ``sep``, ``end``, ``file``, ``flush``. Everything works as |
| 14 | expected in Python: |
| 15 | |
| 16 | .. code-block:: cpp |
| 17 | |
| 18 | py::print(1, 2.0, "three"); // 1 2.0 three |
| 19 | py::print(1, 2.0, "three", "sep"_a="-"); // 1-2.0-three |
| 20 | |
| 21 | auto args = py::make_tuple("unpacked", true); |
| 22 | py::print("->", *args, "end"_a="<-"); // -> unpacked True <- |
| 23 | |
Dean Moldovan | 6d2411f | 2017-04-22 23:24:13 +0200 | [diff] [blame^] | 24 | .. _eval: |
| 25 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 26 | Evaluating Python expressions from strings and files |
| 27 | ==================================================== |
| 28 | |
Dean Moldovan | 076c738 | 2017-04-30 01:53:06 +0200 | [diff] [blame] | 29 | pybind11 provides the `eval`, `exec` and `eval_file` functions to evaluate |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 30 | Python expressions and statements. The following example illustrates how they |
| 31 | can be used. |
| 32 | |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 33 | .. code-block:: cpp |
| 34 | |
| 35 | // At beginning of file |
| 36 | #include <pybind11/eval.h> |
| 37 | |
| 38 | ... |
| 39 | |
| 40 | // Evaluate in scope of main module |
| 41 | py::object scope = py::module::import("__main__").attr("__dict__"); |
| 42 | |
| 43 | // Evaluate an isolated expression |
| 44 | int result = py::eval("my_variable + 10", scope).cast<int>(); |
| 45 | |
| 46 | // Evaluate a sequence of statements |
Dean Moldovan | 076c738 | 2017-04-30 01:53:06 +0200 | [diff] [blame] | 47 | py::exec( |
Dean Moldovan | 67b52d8 | 2016-10-16 19:12:43 +0200 | [diff] [blame] | 48 | "print('Hello')\n" |
| 49 | "print('world!');", |
| 50 | scope); |
| 51 | |
| 52 | // Evaluate the statements in an separate Python file on disk |
| 53 | py::eval_file("script.py", scope); |
Dean Moldovan | 194d8b9 | 2017-03-29 00:27:56 +0200 | [diff] [blame] | 54 | |
| 55 | C++11 raw string literals are also supported and quite handy for this purpose. |
| 56 | The only requirement is that the first statement must be on a new line following |
| 57 | the raw string delimiter ``R"(``, ensuring all lines have common leading indent: |
| 58 | |
| 59 | .. code-block:: cpp |
| 60 | |
Dean Moldovan | 076c738 | 2017-04-30 01:53:06 +0200 | [diff] [blame] | 61 | py::exec(R"( |
Dean Moldovan | 194d8b9 | 2017-03-29 00:27:56 +0200 | [diff] [blame] | 62 | x = get_answer() |
| 63 | if x == 42: |
| 64 | print('Hello World!') |
| 65 | else: |
| 66 | print('Bye!') |
| 67 | )", scope |
| 68 | ); |
Dean Moldovan | 076c738 | 2017-04-30 01:53:06 +0200 | [diff] [blame] | 69 | |
| 70 | .. note:: |
| 71 | |
| 72 | `eval` and `eval_file` accept a template parameter that describes how the |
| 73 | string/file should be interpreted. Possible choices include ``eval_expr`` |
| 74 | (isolated expression), ``eval_single_statement`` (a single statement, return |
| 75 | value is always ``none``), and ``eval_statements`` (sequence of statements, |
| 76 | return value is always ``none``). `eval` defaults to ``eval_expr``, |
| 77 | `eval_file` defaults to ``eval_statements`` and `exec` is just a shortcut |
| 78 | for ``eval<eval_statements>``. |