A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept. There are five kinds of parameter:
positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword argument. This is the default kind of parameter, for example foo and bar in the following:
def func(foo, bar=None): ...
positional-only: specifies an argument that can be supplied only by position. Python has no syntax for defining positional-only parameters. However, some built-in functions have positional-only parameters (e.g. abs()).
keyword-only: specifies an argument that can be supplied only by keyword. Keyword-only parameters can be defined by including a single var-positional parameter or bare * in the parameter list of the function definition before them, for example kw_only1 and kw_only2 in the following:
CPython implementation detail: An implementation may provide built-in functions whose positional parameters do not have names, even if they are ‘named’ for the purpose of documentation, and which therefore cannot be supplied by keyword. In CPython, this is the case for functions implemented in C that use PyArg_ParseTuple() to parse their arguments.
如果您想要默认参数,您应该添加以下内容
因此,如果您在没有参数的情况下调用fun,默认值将使代码不会出错
本教程给出的答案是正确的,它们是位置和关键字参数。考虑下面的例子:
从Python Glossary (parameter)开始:
从另一个意义上说,“位置”也总是正确的,因为关键字参数是作为“幕后”位置参数计算的。从Python language reference (Calls)开始:
同样的docs也给出了位置参数而非关键字参数的示例:
相关问题 更多 >
编程相关推荐