简体   繁体   English

python中的定义foo(*,a = 1)如何与文档引用中的正式“参数列表”匹配?

[英]How does definition foo(*, a=1) in python match the formal 'argument list' in documentation reference?

I've been studying *args and **kwargs in python recently and I've come across that in python 3 one can use bare * as in the example from the title to indicate that no positional arguments are allowed after it. 我最近在python中研究过*args**kwargs ,我在python 3中遇到过,可以使用裸*如标题中的示例所示,表示之后不允许任何位置参数。

The problem is that I don't see how it matches the formal definition or 'argument list' in reference . 问题是我没有看到它与参考文献中的正式定义或“参数列表”的匹配程度如何。 There is a following definition of argument list: 参数列表有以下定义:

argument_list        ::=  positional_arguments ["," keyword_arguments]
                        ["," "*" expression] ["," keyword_arguments]
                        ["," "**" expression]
                      | keyword_arguments ["," "*" expression]
                        ["," keyword_arguments] ["," "**" expression]
                      | "*" expression ["," keyword_arguments] ["," "**" expression]
                      | "**" expression

The only way I see it to be valid is for expression to be empty, but I couldn't see how it can happen. 我认为它有效的唯一方法是expression为空,但我看不出它是如何发生的。

In case we assume that expression can be empty then the following definitions should be valid: 如果我们假设expression可以为空,那么以下定义应该是有效的:

def foo(*)
def bar(*,**kwargs)

Those definitions don't make sense since * requires keyword arguments after it. 这些定义没有意义,因为*后面需要关键字参数。

So I would appreciate any clarifications on the subject. 所以我很感激有关这个问题的任何澄清。 What do I miss? 我错过了什么? Or is the above BNF not quite correct? 或者上面的BNF不太正确?

The part of the grammar you're looking at describes function calls . 您正在查看的语法部分描述了函数调用 And a bare * is indeed invalid in a function call. 并且在函数调用中确实无效*

The function definition grammar is in another part of the language reference. 函数定义语法是语言参考的另一部分。 These productions are relevant: 这些作品是相关的:

parameter_list ::=  (defparameter ",")*
                    ( "*" [parameter] ("," defparameter)* ["," "**" parameter]
                    | "**" parameter
                    | defparameter [","] )
parameter      ::=  identifier [":" expression]
defparameter   ::=  parameter ["=" expression]

Note that here, parameter s and defparameters are optional after after the * . 注意,这里, parameter S和defparameters后后是可选的*

The error that complains about def f(*) and def f(*, **kwds) does not come from the grammar. 抱怨def f(*)def f(*, **kwds)不是来自语法。 It's legal as far as the grammar is concerned, and is parsed successfully. 就语法而言,它是合法的,并且被成功解析。 Only during AST generation this is detected and flagged as error. 在AST生成期间,检测到该标记并将其标记为错误。 This isn't the only syntactical constraint that isn't handled in the grammar. 这不是语法中没有处理的唯一语法约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 3中的lambda如何处理参数引用? - How does lambda in python 3 handle the argument reference? 如何在Python中使用正则表达式匹配列表引用? - How to I regex match a list reference in Python? “ foo(* a)”在Python中如何工作? - How does “foo(*a)” work in Python? 如何使用 ctypes 模块通过引用 EnumChildWindows 将 Python 列表作为参数发送? - How to send a Python list as argument by reference to EnumChildWindows using the ctypes module? Sphinx 文档:如何引用 Python 属性? - Sphinx documentation: how to reference a Python property? os.removexattr的Python文档 - '*'(star)参数是什么意思? - Python documentation for os.removexattr — what does the '*' (star) argument mean? 有关格式的python文档与运行结果不匹配 - The python documentation about format does not match the running results 在Python中将数组/列表的子集引用作为参数传递 - Passing subset reference of array/list as an argument in Python 将半结构化文本列表转换为正式定义 - Convert a list of semi-structured text into a formal definition Python Twitter API结果与文档不匹配:“ URL”字段 - Python Twitter API result does not match documentation: 'urls' field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM