blob: 171a885248d59d0eea3df1397c29d3c4ac927730 [file] [log] [blame]
Dean Moldovan67b52d82016-10-16 19:12:43 +02001Utilities
2#########
3
4Using Python's print function in C++
5====================================
6
7The usual way to write output in C++ is using ``std::cout`` while in Python one
8would use ``print``. Since these methods use different buffers, mixing them can
9lead 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
12Python's ``print`` function is replicated in the C++ API including optional
13keyword arguments ``sep``, ``end``, ``file``, ``flush``. Everything works as
14expected 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 Moldovan6d2411f2017-04-22 23:24:13 +020024.. _eval:
25
Dean Moldovan67b52d82016-10-16 19:12:43 +020026Evaluating Python expressions from strings and files
27====================================================
28
Dean Moldovan076c7382017-04-30 01:53:06 +020029pybind11 provides the `eval`, `exec` and `eval_file` functions to evaluate
Dean Moldovan67b52d82016-10-16 19:12:43 +020030Python expressions and statements. The following example illustrates how they
31can be used.
32
Dean Moldovan67b52d82016-10-16 19:12:43 +020033.. 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 Moldovan076c7382017-04-30 01:53:06 +020047 py::exec(
Dean Moldovan67b52d82016-10-16 19:12:43 +020048 "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 Moldovan194d8b92017-03-29 00:27:56 +020054
55C++11 raw string literals are also supported and quite handy for this purpose.
56The only requirement is that the first statement must be on a new line following
57the raw string delimiter ``R"(``, ensuring all lines have common leading indent:
58
59.. code-block:: cpp
60
Dean Moldovan076c7382017-04-30 01:53:06 +020061 py::exec(R"(
Dean Moldovan194d8b92017-03-29 00:27:56 +020062 x = get_answer()
63 if x == 42:
64 print('Hello World!')
65 else:
66 print('Bye!')
67 )", scope
68 );
Dean Moldovan076c7382017-04-30 01:53:06 +020069
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>``.