Skip to content

Commit fa4b4e8

Browse files
committed
Document calling function with keyword arguments from C++
1 parent a12095c commit fa4b4e8

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

docs/advanced.rst

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,24 +1589,56 @@ It is also possible to call python functions via ``operator()``.
15891589
py::object result_py = f(1234, "hello", some_instance);
15901590
MyClass &result = result_py.cast<MyClass>();
15911591
1592-
The special ``f(*args)`` and ``f(*args, **kwargs)`` syntax is also supported to
1593-
supply arbitrary argument and keyword lists, although these cannot be mixed
1594-
with other parameters.
1592+
Keyword arguments are also supported. In Python, there is the usual call syntax:
1593+
1594+
.. code-block:: python
1595+
1596+
def f(number, say, to):
1597+
... # function code
1598+
1599+
f(1234, say="hello", to=some_instance) # keyword call in Python
1600+
1601+
In C++, the same call can be made using:
15951602

15961603
.. code-block:: cpp
15971604
1598-
py::function f = <...>;
1605+
using pybind11::literals; // to bring in the `_a` literal
1606+
f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
1607+
1608+
Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with
1609+
other arguments:
1610+
1611+
.. code-block:: cpp
1612+
1613+
// * unpacking
1614+
py::tuple args = py::make_tuple(1234, "hello", some_instance);
1615+
f(*args);
1616+
1617+
// ** unpacking
1618+
py::dict kwargs = py::dict("number"_a=1234, "say"_a="hello", "to"_a=some_instance);
1619+
f(**kwargs);
1620+
1621+
// mixed keywords, * and ** unpacking
15991622
py::tuple args = py::make_tuple(1234);
1600-
py::dict kwargs;
1601-
kwargs["y"] = py::cast(5678);
1602-
py::object result = f(*args, **kwargs);
1623+
py::dict kwargs = py::dict("to"_a=some_instance);
1624+
f(*args, "say"_a="hello", **kwargs);
1625+
1626+
Generalized unpacking according to PEP448_ is also supported:
1627+
1628+
.. code-block:: cpp
1629+
1630+
py::dict kwargs1 = py::dict("number"_a=1234);
1631+
py::dict kwargs2 = py::dict("to"_a=some_instance);
1632+
f(**kwargs1, "say"_a="hello", **kwargs2);
16031633
16041634
.. seealso::
16051635

16061636
The file :file:`tests/test_python_types.cpp` contains a complete
16071637
example that demonstrates passing native Python types in more detail. The
1608-
file :file:`tests/test_kwargs_and_defaults.cpp` discusses usage
1609-
of ``args`` and ``kwargs``.
1638+
file :file:`tests/test_callbacks.cpp` presents a few examples of calling
1639+
Python functions from C++, including keywords arguments and unpacking.
1640+
1641+
.. _PEP448: https://www.python.org/dev/peps/pep-0448/
16101642

16111643
Default arguments revisited
16121644
===========================

docs/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ Breaking changes queued for v2.0.0 (Not yet released)
4646
* Added constructors for ``str`` and ``bytes`` from zero-terminated char pointers,
4747
and from char pointers and length.
4848
* Added ``memoryview`` wrapper type which is constructible from ``buffer_info``.
49+
* New syntax to call a Python function from C++ using keyword arguments and unpacking,
50+
e.g. ``foo(1, 2, "z"_a=3)`` or ``bar(1, *args, "z"_a=3, **kwargs)``.
51+
* Added ``py::print()`` function which replicates Python's API and writes to Python's
52+
``sys.stdout`` by default (as opposed to C's ``stdout`` like ``std::cout``).
53+
* Added ``py::dict`` keyword constructor:``auto d = dict("number"_a=42, "name"_a="World");``
54+
* Added ``py::str::format()`` method and ``_s`` literal:
55+
``py::str s = "1 + 2 = {}"_s.format(3);``
4956
* Various minor improvements of library internals (no user-visible changes)
5057

5158
1.8.1 (July 12, 2016)

0 commit comments

Comments
 (0)