Python 为什么非默认参数不能跟随默认参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16932825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:02:22  来源:igfitidea点击:

Why can't non-default arguments follow default arguments?

python

提问by dotslash

Why does this piece of code throw a SyntaxError?

为什么这段代码会抛出 SyntaxError?

  >>> def fun1(a="who is you", b="True", x, y):
...     print a,b,x,y
... 
  File "<stdin>", line 1
SyntaxError: non-default argument follows default argument

While the following piece of code runs without visible errors:

虽然以下代码运行时没有明显错误:

>>> def fun1(x, y, a="who is you", b="True"):
...     print a,b,x,y
... 

采纳答案by Rahul Gautam

All required parameters must be placed before any default arguments. Simply because they are mandatory, whereas default arguments are not. Syntactically, it would be impossiblefor the interpreter to decide which values match which arguments if mixed modes were allowed. A SyntaxErroris raised if the arguments are not given in the correct order:

所有必需的参数都必须放在任何默认参数之前。仅仅因为它们是强制性的,而默认参数不是。从语法上讲,如果允许混合模式,解释器就不可能决定哪些值与哪些参数匹配。SyntaxError如果参数未按正确顺序给出,则会引发A :

Let us take a look at keyword arguments, using your function.

让我们使用您的函数来看看关键字参数。

def fun1(a="who is you", b="True", x, y):
...     print a,b,x,y

Suppose its allowed to declare function as above, Then with the above declarations, we can make the following (regular) positional or keyword argument calls:

假设它允许像上面那样声明函数,然后通过上面的声明,我们可以进行以下(常规)位置或关键字参数调用:

func1("ok a", "ok b", 1)  # Is 1 assigned to x or ?
func1(1)                  # Is 1 assigned to a or ?
func1(1, 2)               # ?

How you will suggest the assignment of variables in the function call, how default arguments are going to be used along with keyword arguments.

您将如何建议在函数调用中分配变量,如何将默认参数与关键字参数一起使用。

>>> def fun1(x, y, a="who is you", b="True"):
...     print a,b,x,y
... 

Reference O'Reilly - Core-Python
Where as this function make use of the default arguments syntactically correct for above function calls. Keyword arguments calling prove useful for being able to provide for out-of-order positional arguments, but, coupled with default arguments, they can also be used to "skip over" missing arguments as well.

参考 O'Reilly - Core-Python
其中该函数使用语法正确的默认参数对上述函数调用。关键字参数调用证明能够提供乱序位置参数很有用,但是,加上默认参数,它们也可以用来“跳过”缺少的参数。

回答by cforbish

Required arguments (the ones without defaults), must be at the start to allow client code to only supply two. If the optional arguments were at the start, it would be confusing:

必需参数(没有默认值的参数)必须在开头,以允许客户端代码仅提供两个。如果可选参数在开头,则会令人困惑:

fun1("who is who", 3, "Hyman")

What would that do in your first example? In the last, x is "who is who", y is 3 and a = "Hyman".

在你的第一个例子中会做什么?最后,x 是“who is who”,y 是 3 和 a = “Hyman”。

回答by jamylak

SyntaxError: non-default argument follows default argument

If you were to allow this, the default arguments would be rendered useless because you would never be able to use their default values, since the non-default arguments come after.

如果您允许这样做,默认参数将变得无用,因为您将永远无法使用它们的默认值,因为非默认参数.

In Python 3 however, you may do the following:

但是,在 Python 3 中,您可以执行以下操作:

def fun1(a="who is you", b="True", *, x, y):
    pass

which makes xand ykeyword only so you can do this:

这使得xy关键字只有这样你就可以做到这一点:

fun1(x=2, y=2)

This works because there is no longer any ambiguity. Note you still can't do fun1(2, 2)(that would set the default arguments).

这是有效的,因为不再有任何歧义。请注意,您仍然不能这样做fun1(2, 2)(这将设置默认参数)。

回答by Aaditya Ura

Let me clear two points here :

让我在这里澄清两点:

  • firstly non-default argument should not follow default argument , it means you can't define (a=b,c) in function the order of defining parameter in function are :
    • positional parameter or non-default parameter i.e (a,b,c)
    • keyword parameter or default parameter i.e (a="b",r="j")
    • keyword-only parameter i.e (*args)
    • var-keyword parameter i.e (**kwargs)
  • 首先非默认参数不应跟随默认参数,这意味着您不能在函数中定义 (a=b,c) 函数中定义参数的顺序是:
    • 位置参数或非默认参数即 (a,b,c)
    • 关键字参数或默认参数即 (a="b",r="j")
    • 仅关键字参数即 (*args)
    • var-keyword 参数即 (**kwargs)

def example(a, b, c=None, r="w" , d=[], *ae, **ab):

def example(a, b, c=None, r="w" , d=[], *ae, **ab):

(a,b) are positional parameter

(a,b) 是位置参数

(c=none) is optional parameter

(c=none) 是可选参数

(r="w") is keyword parameter

(r="w") 是关键字参数

(d=[]) is list parameter

(d=[]) 是列表参数

(*ae) is keyword-only

(*ae) 仅是关键字

(**ab) is var-keyword parameter

(**ab) 是 var-keyword 参数

  • now secondary thing is if i try something like this : def example(a, b, c=a,d=b):
  • 现在次要的是,如果我尝试这样的事情: def example(a, b, c=a,d=b):

argument is not defined when default values are saved,Python computes and saves default values when you define the function

保存默认值时未定义参数,Python在定义函数时计算并保存默认值

c and d are not defined, does not exist, when this happens (it exists only when the function is executed)

c 和 d 未定义,不存在,发生这种情况时(仅在执行函数时才存在)

"a,a=b" its not allowed in parameter.

"a,a=b" 它不允许在参数中。