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
96017dd7
Commit
96017dd7
authored
Jun 03, 2016
by
Dean Moldovan
Browse files
Add _a literal for named arguments
parent
f600c1d8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
8 deletions
+27
-8
example/example11.cpp
example/example11.cpp
+3
-0
example/example11.py
example/example11.py
+4
-1
example/example11.ref
example/example11.ref
+7
-0
include/pybind11/attr.h
include/pybind11/attr.h
+13
-7
No files found.
example/example11.cpp
View file @
96017dd7
...
@@ -55,4 +55,7 @@ void init_ex11(py::module &m) {
...
@@ -55,4 +55,7 @@ void init_ex11(py::module &m) {
m
.
def
(
"args_function"
,
&
args_function
);
m
.
def
(
"args_function"
,
&
args_function
);
m
.
def
(
"args_kwargs_function"
,
&
args_kwargs_function
);
m
.
def
(
"args_kwargs_function"
,
&
args_kwargs_function
);
using
namespace
py
::
literals
;
m
.
def
(
"kw_func_udl"
,
&
kw_func
,
"x"
_a
,
"y"
_a
=
300
);
}
}
example/example11.py
View file @
96017dd7
...
@@ -6,12 +6,13 @@ import pydoc
...
@@ -6,12 +6,13 @@ import pydoc
sys
.
path
.
append
(
'.'
)
sys
.
path
.
append
(
'.'
)
from
example
import
kw_func
,
kw_func2
,
kw_func3
,
kw_func4
,
call_kw_func
from
example
import
kw_func
,
kw_func2
,
kw_func3
,
kw_func4
,
call_kw_func
from
example
import
args_function
,
args_kwargs_function
from
example
import
args_function
,
args_kwargs_function
,
kw_func_udl
print
(
pydoc
.
render_doc
(
kw_func
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func2
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func2
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func3
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func3
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func4
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func4
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
kw_func_udl
,
"Help on %s"
))
kw_func
(
5
,
10
)
kw_func
(
5
,
10
)
kw_func
(
5
,
y
=
10
)
kw_func
(
5
,
y
=
10
)
...
@@ -39,3 +40,5 @@ call_kw_func(kw_func2)
...
@@ -39,3 +40,5 @@ call_kw_func(kw_func2)
args_function
(
'arg1_value'
,
'arg2_value'
,
3
)
args_function
(
'arg1_value'
,
'arg2_value'
,
3
)
args_kwargs_function
(
'arg1_value'
,
'arg2_value'
,
arg3
=
'arg3_value'
,
arg4
=
4
)
args_kwargs_function
(
'arg1_value'
,
'arg2_value'
,
arg3
=
'arg3_value'
,
arg4
=
4
)
kw_func_udl
(
x
=
5
,
y
=
10
)
example/example11.ref
View file @
96017dd7
...
@@ -18,6 +18,11 @@ Help on built-in function kw_func4 in module example
...
@@ -18,6 +18,11 @@ Help on built-in function kw_func4 in module example
kkww__ffuunncc44(...)
kkww__ffuunncc44(...)
kw_func4(myList : list<int> = [13L, 17L]) -> NoneType
kw_func4(myList : list<int> = [13L, 17L]) -> NoneType
Help on built-in function kw_func_udl in module example
kkww__ffuunncc__uuddll(...)
kw_func_udl(x : int, y : int = 300L) -> NoneType
kw_func(x=5, y=10)
kw_func(x=5, y=10)
kw_func(x=5, y=10)
kw_func(x=5, y=10)
kw_func(x=5, y=10)
kw_func(x=5, y=10)
...
@@ -40,3 +45,5 @@ got argument: arg1_value
...
@@ -40,3 +45,5 @@ got argument: arg1_value
got argument: arg2_value
got argument: arg2_value
got keyword argument: arg3 -> arg3_value
got keyword argument: arg3 -> arg3_value
got keyword argument: arg4 -> 4
got keyword argument: arg4 -> 4
kw_func(x=5, y=10)
include/pybind11/attr.h
View file @
96017dd7
...
@@ -18,23 +18,29 @@ template <typename T> struct arg_t;
...
@@ -18,23 +18,29 @@ template <typename T> struct arg_t;
/// Annotation for keyword arguments
/// Annotation for keyword arguments
struct
arg
{
struct
arg
{
arg
(
const
char
*
name
)
:
name
(
name
)
{
}
constexpr
arg
(
const
char
*
name
)
:
name
(
name
)
{
}
template
<
typename
T
>
arg_t
<
T
>
operator
=
(
const
T
&
value
);
template
<
typename
T
,
size_t
N
>
arg_t
<
const
T
*>
operator
=
(
T
const
(
&
value
)[
N
]);
template
<
typename
T
>
constexpr
arg_t
<
T
>
operator
=
(
const
T
&
value
)
const
{
return
{
name
,
value
};
}
template
<
typename
T
,
size_t
N
>
constexpr
arg_t
<
const
T
*>
operator
=
(
T
const
(
&
value
)[
N
])
const
{
return
operator
=
((
const
T
*
)
value
);
};
const
char
*
name
;
const
char
*
name
;
};
};
/// Annotation for keyword arguments with default values
/// Annotation for keyword arguments with default values
template
<
typename
T
>
struct
arg_t
:
public
arg
{
template
<
typename
T
>
struct
arg_t
:
public
arg
{
arg_t
(
const
char
*
name
,
const
T
&
value
,
const
char
*
descr
=
nullptr
)
constexpr
arg_t
(
const
char
*
name
,
const
T
&
value
,
const
char
*
descr
=
nullptr
)
:
arg
(
name
),
value
(
value
),
descr
(
descr
)
{
}
:
arg
(
name
),
value
(
value
),
descr
(
descr
)
{
}
T
value
;
T
value
;
const
char
*
descr
;
const
char
*
descr
;
};
};
template
<
typename
T
>
arg_t
<
T
>
arg
::
operator
=
(
const
T
&
value
)
{
return
arg_t
<
T
>
(
name
,
value
);
}
inline
namespace
literals
{
template
<
typename
T
,
size_t
N
>
arg_t
<
const
T
*>
arg
::
operator
=
(
T
const
(
&
value
)[
N
])
{
/// String literal version of arg
return
operator
=
(
(
const
T
*
)
value
);
constexpr
arg
operator
""
_a
(
const
char
*
name
,
size_t
)
{
return
{
name
};
}
}
}
/// Annotation for methods
/// Annotation for methods
...
...
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