Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
835fc06c
Commit
835fc06c
authored
Jun 16, 2016
by
Brad Harmon
Browse files
Add callback examples with named parameters
parent
a62b3a07
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
2 deletions
+26
-2
docs/advanced.rst
docs/advanced.rst
+16
-2
example/example5.cpp
example/example5.cpp
+6
-0
example/example5.py
example/example5.py
+3
-0
example/example5.ref
example/example5.ref
+1
-0
No files found.
docs/advanced.rst
View file @
835fc06c
...
@@ -139,8 +139,19 @@ its return value upon execution.
...
@@ -139,8 +139,19 @@ its return value upon execution.
};
};
}
}
This example demonstrates using python named parameters in C++ callbacks which
requires using ``py::cpp_function`` as a wrapper. Usage is similar to defining
methods of classes:
.. code-block:: cpp
py::cpp_function func_cpp() {
return py::cpp_function([](int i) { return i+1; },
py::arg("number"));
}
After including the extra header file :file:`pybind11/functional.h`, it is almost
After including the extra header file :file:`pybind11/functional.h`, it is almost
trivial to generate binding code for
both
of these functions.
trivial to generate binding code for
all
of these functions.
.. code-block:: cpp
.. code-block:: cpp
...
@@ -151,6 +162,7 @@ trivial to generate binding code for both of these functions.
...
@@ -151,6 +162,7 @@ trivial to generate binding code for both of these functions.
m.def("func_arg", &func_arg);
m.def("func_arg", &func_arg);
m.def("func_ret", &func_ret);
m.def("func_ret", &func_ret);
m.def("func_cpp", &func_cpp);
return m.ptr();
return m.ptr();
}
}
...
@@ -169,7 +181,9 @@ The following interactive session shows how to call them from Python.
...
@@ -169,7 +181,9 @@ The following interactive session shows how to call them from Python.
>>> square_plus_1 = example.func_ret(square)
>>> square_plus_1 = example.func_ret(square)
>>> square_plus_1(4)
>>> square_plus_1(4)
17L
17L
>>>
>>> plus_1 = func_cpp()
>>> plus_1(number=43)
44L
.. note::
.. note::
...
...
example/example5.cpp
View file @
835fc06c
...
@@ -60,6 +60,11 @@ std::function<int(int)> test_callback4() {
...
@@ -60,6 +60,11 @@ std::function<int(int)> test_callback4() {
return
[](
int
i
)
{
return
i
+
1
;
};
return
[](
int
i
)
{
return
i
+
1
;
};
}
}
py
::
cpp_function
test_callback5
()
{
return
py
::
cpp_function
([](
int
i
)
{
return
i
+
1
;
},
py
::
arg
(
"number"
));
}
void
init_ex5
(
py
::
module
&
m
)
{
void
init_ex5
(
py
::
module
&
m
)
{
py
::
class_
<
Pet
>
pet_class
(
m
,
"Pet"
);
py
::
class_
<
Pet
>
pet_class
(
m
,
"Pet"
);
pet_class
pet_class
...
@@ -82,6 +87,7 @@ void init_ex5(py::module &m) {
...
@@ -82,6 +87,7 @@ void init_ex5(py::module &m) {
m
.
def
(
"test_callback2"
,
&
test_callback2
);
m
.
def
(
"test_callback2"
,
&
test_callback2
);
m
.
def
(
"test_callback3"
,
&
test_callback3
);
m
.
def
(
"test_callback3"
,
&
test_callback3
);
m
.
def
(
"test_callback4"
,
&
test_callback4
);
m
.
def
(
"test_callback4"
,
&
test_callback4
);
m
.
def
(
"test_callback5"
,
&
test_callback5
);
/* Test cleanup of lambda closure */
/* Test cleanup of lambda closure */
...
...
example/example5.py
View file @
835fc06c
...
@@ -29,6 +29,7 @@ from example import test_callback1
...
@@ -29,6 +29,7 @@ from example import test_callback1
from
example
import
test_callback2
from
example
import
test_callback2
from
example
import
test_callback3
from
example
import
test_callback3
from
example
import
test_callback4
from
example
import
test_callback4
from
example
import
test_callback5
from
example
import
test_cleanup
from
example
import
test_cleanup
def
func1
():
def
func1
():
...
@@ -49,5 +50,7 @@ print(test_callback1(partial(func3, "Partial object with one argument")))
...
@@ -49,5 +50,7 @@ print(test_callback1(partial(func3, "Partial object with one argument")))
test_callback3
(
lambda
i
:
i
+
1
)
test_callback3
(
lambda
i
:
i
+
1
)
f
=
test_callback4
()
f
=
test_callback4
()
print
(
"func(43) = %i"
%
f
(
43
))
print
(
"func(43) = %i"
%
f
(
43
))
f
=
test_callback5
()
print
(
"func(number=43) = %i"
%
f
(
number
=
43
))
test_cleanup
()
test_cleanup
()
example/example5.ref
View file @
835fc06c
...
@@ -24,3 +24,4 @@ False
...
@@ -24,3 +24,4 @@ False
Callback function 3 called : Partial object with one argument
Callback function 3 called : Partial object with one argument
False
False
func(43) = 44
func(43) = 44
func(number=43) = 44
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment