Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在python中,Kwarg不算作命名参数?_Python_Python 3.x - Fatal编程技术网

为什么在python中,Kwarg不算作命名参数?

为什么在python中,Kwarg不算作命名参数?,python,python-3.x,Python,Python 3.x,为什么在python中不能执行以下操作: def foo(bar, *, **kwargs): pass 但我们可以做到: def foo(bar, *, baz='qux', **kwargs): pass 或者换句话说,为什么python不将**kwargs计算为命名参数?Bare*用于强制调用方使用命名参数-因此,如果没有以下关键字参数,则无法将带有*的函数定义为参数 只是添加一些相关信息。单个星号的行为源自PEP-3102。引用相关章节: The second sy

为什么在python中不能执行以下操作:

def foo(bar, *, **kwargs):
    pass
但我们可以做到:

def foo(bar, *, baz='qux', **kwargs): 
    pass

或者换句话说,为什么python不将
**kwargs
计算为命名参数

Bare
*
用于强制调用方使用命名参数-因此,如果没有以下关键字参数,则无法将带有
*
的函数定义为参数

只是添加一些相关信息。单个星号的行为源自PEP-3102。引用相关章节:

The second syntactical change is to allow the argument name to
be omitted for a varargs argument. The meaning of this is to
allow for keyword-only arguments for functions that would not
otherwise take a varargs argument:

def compare(a, b, *, key=None):
    ...
这总结了已经结束的问题:


*
的对应项是“/”。作为一个参数,ot标记仅位置参数()的结束,即不能用作关键字参数的参数。在eq(self,value,/)的情况下,斜杠位于末尾,这意味着所有参数都标记为仅位置参数,而对于init而言,只有self才是仅位置参数。

Bare
*
用于强制调用方使用命名参数-因此,如果没有以下关键字参数,则无法将带有
*
的函数定义为参数

只是添加一些相关信息。单个星号的行为源自PEP-3102。引用相关章节:

The second syntactical change is to allow the argument name to
be omitted for a varargs argument. The meaning of this is to
allow for keyword-only arguments for functions that would not
otherwise take a varargs argument:

def compare(a, b, *, key=None):
    ...
这总结了已经结束的问题:


*
的对应项是“/”。作为一个参数,ot标记仅位置参数()的结束,即不能用作关键字参数的参数。在eq(self,value,/)的情况下,斜杠在末尾,这意味着所有参数都标记为仅位置参数,而在init的情况下,仅self是仅位置参数。

这与
def foo(**kwargs):
?@MrFuppes修订版是否明确了这一点,我想你说得有道理。另外,我不认为dupe标记的问题可以解释这一点。@juanpa.arrivillaga:在PEP 3102中,有没有解释为什么
*
后面不能跟
**kwargs
?我参加聚会迟到了,但我不会
def foo(bar,*,**kwargs):
只是多余的<代码>*强制调用方仅在该点之后使用命名参数
**kwargs
执行相同的操作,但将命名的参数作为
dict
使用到
kwargs
。你做前者的理由是什么?这与
def foo(**kwargs):
?@MrFuppes修订版是否明确了?是的,我认为你的观点是正确的。另外,我不认为dupe标记的问题可以解释这一点。@juanpa.arrivillaga:在PEP 3102中,有没有解释为什么
*
后面不能跟
**kwargs
?我参加聚会迟到了,但我不会
def foo(bar,*,**kwargs):
只是多余的<代码>*强制调用方仅在该点之后使用命名参数
**kwargs
执行相同的操作,但将命名的参数作为
dict
使用到
kwargs
。你有什么理由做前者?