python3 SyntaxError: non-default argument follows default argumentはなぜ起こるのか


default argumentとは


def myfunc(value=None):
    if value is None:
        value = []
    # modify value here


引数の数 > 1という前提において、default argument(def foo(x, y, default_arg=1): return default_arg, y, x,←の一番右の引数)はルールとして最初の引数として配置できませんよと言う意味。



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

defaultなのかそうでないのかの線引きをするためにdefault argumentは最後に配置するよう定められているわけだ。


Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.