1
2
3
4
5
6
7
| >>> def foo(a,b, * ,c,d): ... print (a,b,c,d) ... >>> foo( 1 , 2 , 3 , 4 ) Traceback (most recent call last): File " , line 1 , in
TypeError: foo() takes exactly 2 positional arguments ( 4 given) |
In this case, everything after the * is required to be keyword only. This means that you can safely amend the function definition to re-order or even add keyword arguments.
Likewise, the following requires ALL keyword arguments.
1
2
3
4
5
6
7
8
9
10
| >>> def foo( * ,a,b): ... print (a,b) ... >>> foo( 1 , 2 ) Traceback (most recent call last): File " , line 1 , in
TypeError: foo() takes exactly 0 positional arguments ( 2 given) >>> foo(a = 1 , b = 2 ) 1 2 |
Comments
Post a Comment
https://gengwg.blogspot.com/