*[ keyword_arg = value] 在 Python 关键字函数定义中做了什么?
What does *[, keyword_arg = value] do in Python keyword only function definition?
考虑一下:
help(min)
给出:
Help on built-in function min in module builtins:
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
我的问题是有什么区别:
min(iterable, *[, default=obj, key=func]) -> value
和
min(iterable, *, default=obj, key=func) -> value
这不是 Python 语法,它只是用在帮助文本中以表明这些参数是可选的。
Python documentation/help 派生自 EBNF 来描述语法。一对 [
]
在 EBNF 和 Python 文档中表示“可选”;为简洁起见,文档中未引用 (
)
等常见符号和名称,即使这在 EBNF 中具有不同的含义。
*
和 ,
派生自 Python 的 call syntax。 *
表示“位置参数结束”。
具体例子
min(iterable, *[, default=obj, key=func]) -> value
这意味着 default
和 key
是可选的仅关键字参数。
考虑一下:
help(min)
给出:
Help on built-in function min in module builtins:
min(...)
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
我的问题是有什么区别:
min(iterable, *[, default=obj, key=func]) -> value
和
min(iterable, *, default=obj, key=func) -> value
这不是 Python 语法,它只是用在帮助文本中以表明这些参数是可选的。
Python documentation/help 派生自 EBNF 来描述语法。一对 [
]
在 EBNF 和 Python 文档中表示“可选”;为简洁起见,文档中未引用 (
)
等常见符号和名称,即使这在 EBNF 中具有不同的含义。
*
和 ,
派生自 Python 的 call syntax。 *
表示“位置参数结束”。
具体例子
min(iterable, *[, default=obj, key=func]) -> value
这意味着 default
和 key
是可选的仅关键字参数。