ChatGPT解决这个技术问题 Extra ChatGPT

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

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
... 
in python that is how the structure is when you define a function def myfunction(position_arguments, *arguments, **keywords): ` ....`
There is no sensible answer to this when it comes to KEYWORD-ONLY arguments - why can't default and non-default keyword-only arguments be mixed? They're keyword, so their order shouldn't really matter. This IS an arbitrary limitation.

k
kirelagin

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.


<q>Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed.</q> Ruby allows non-default arguments following default ones. <q>How you will suggest the assignment of variables in the function call</q> Just use func1(x=1, y=2).
Hi @weakish, thanks for the comment, Python does have a nice way to achieve this, Please go search for *args and **kwargs
Hi @Rahul Gautam I'm confused. I thought **kwargs can not make def fun1(a="who is you", b="True", x, y) or similar work. If you use def f(**kwargs), you need to handle default values in the body?
Why do you write" Syntactically, it would be impossible for the interpreter to decide which values match which arguments if mixed modes were allowed."? I think that the algorithm at PEP 3102 could work exactly the same even if def foo(x=None, y) would be allowed. So I'm still curious why this is not allowed by Python. Is there a real fundamental reason or is it just "we don't allow it because we don't allow it".
try with def foo(a=1 ,b): called via foo(b=2) and tell me where the pep throws, it is not the cause to forbid defaults before nondefaults i guess..
j
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:

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


Keyword-only arguments are a neat feature of Python 3. Too bad the patch to backport them to Python 2.6 and 2.7 was just left to expire.
This is not true: "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." Example: def foo(a=1 ,b): called via foo(b=2)
A
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)

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.


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

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