问题:为什么非默认参数不能跟随默认参数?

为什么这段代码会引发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

尽管以下代码段运行时没有可见错误:

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

Why does this piece of code throw a 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
... 

回答 0

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

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

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

假设其允许声明函数如上,然后使用上述声明,我们可以进行以下(常规)位置或关键字参数调用:

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

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

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

参考O’Reilly-Core-Python
其中,此函数在语法上正确地使用了上述函数调用的默认参数。事实证明,关键字参数调用对于提供乱序的位置参数很有用,但是与默认参数一起使用,它们也可以用于“跳过”缺少的参数。

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

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.


回答 1

SyntaxError: non-default argument follows default argument

如果允许这样做,则默认参数将变得无用,因为您将永远无法使用其默认值,因为非默认参数会在后面

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

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

这使得xy关键字只有这样,你可以这样做:

fun1(x=2, y=2)

之所以可行,是因为不再有任何歧义。请注意,您仍然无法执行操作fun1(2, 2)(这将设置默认参数)。

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:

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

which makes x and y keyword only so you can do this:

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).


回答 2

让我在这里澄清两点:

  • 首先,非默认参数不应跟随默认参数,这意味着您无法在函数中定义(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):

(a,b)是位置参数

(c = none)是可选参数

(r =“ w”)是关键字参数

(d = [])是列表参数

(* ae)仅用于关键字

(** ab)是var-keyword参数

  • 现在第二件事是,如果我尝试这样的事情: def example(a, b, c=a,d=b):

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

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

参数“ a,a = b”不允许使用。

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)

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

(a,b) are positional parameter

(c=none) is optional parameter

(r=”w”) is keyword parameter

(d=[]) is list parameter

(*ae) is keyword-only

(**ab) is var-keyword parameter

  • now secondary thing is if i try something like this : 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

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

“a,a=b” its not allowed in parameter.


回答 3

必需的参数(没有默认值的参数)必须在开始时才允许客户端代码仅提供两个。如果可选参数是开头,那将会造成混乱:

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

在您的第一个示例中该怎么做?最后,x是“谁是谁”,y是3,a =“ jack”。

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, "jack")

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。