- function parameter Pattern matching overview
| syntax |
position |
explanation |
| func (value |
Caller |
General parameters: Location matching |
| func (name= Value) |
Caller |
Keyword parameter: Key value matching |
| func (*SEQ) |
Caller |
Unpack the sequence as a regular parameter and pass the parameter |
| func (* * dict) |
Caller |
Unpack the dictionary as a keyword parameter and pass the parameter |
| def func ( Name) |
function definition |
General parameters: Location matching or variable name matching |
| def func ( Name=value) |
function definition |
Set default parameter value |
| def func (* Name) |
function definition |
Collect parameter composition sequence |
| def func (* * Name) |
function definition |
Collect parameter composition dictionary |
| def func (* args, name) |
function definition |
Force the name parameter to pass the | with the keyword
- Use the * parameter to force the use of the keyword-only parameter and indicate that a variable quantity parameter is not accepted. Compare the following example:
# 1def f(*, a):return aprint(f(1, a=‘kopara‘)) # => error# 2def f(*_, a):return aprint(f(1, a=‘kopara‘)) # => kopara
- The keyword parameter must be preceded by the dictionary collection/unpacking parameters to avoid ambiguity.
?
[Python] Function parameter small note