Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | example/example11.cpp -- keyword arguments and default values |
| 3 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "example.h" |
Wenzel Jakob | 9180519 | 2016-01-17 22:36:43 +0100 | [diff] [blame] | 11 | #include <pybind11/stl.h> |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 12 | |
| 13 | void kw_func(int x, int y) { std::cout << "kw_func(x=" << x << ", y=" << y << ")" << std::endl; } |
| 14 | |
Wenzel Jakob | 9180519 | 2016-01-17 22:36:43 +0100 | [diff] [blame] | 15 | void kw_func4(const std::vector<int> &entries) { |
| 16 | std::cout << "kw_func4: "; |
| 17 | for (int i : entries) |
| 18 | std::cout << i << " "; |
| 19 | std::cout << endl; |
| 20 | } |
| 21 | |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 22 | void init_ex11(py::module &m) { |
| 23 | m.def("kw_func", &kw_func, py::arg("x"), py::arg("y")); |
| 24 | m.def("kw_func2", &kw_func, py::arg("x") = 100, py::arg("y") = 200); |
Wenzel Jakob | 66c9a40 | 2016-01-17 22:36:36 +0100 | [diff] [blame] | 25 | m.def("kw_func3", [](const char *) { }, py::arg("data") = std::string("Hello world!")); |
Wenzel Jakob | 9180519 | 2016-01-17 22:36:43 +0100 | [diff] [blame] | 26 | |
| 27 | /* A fancier default argument */ |
| 28 | std::vector<int> list; |
| 29 | list.push_back(13); |
| 30 | list.push_back(17); |
| 31 | |
| 32 | m.def("kw_func4", &kw_func4, py::arg("myList") = list); |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 33 | } |