SyntaxError: non-default argument follows default argument

后端 未结 3 1866
离开以前
离开以前 2020-12-07 13:05
from os import system
def a(len1,hgt=len1,til,col=0):
    system(\'mode con cols=\'+len1,\'lines=\'+hgt)
    system(\'title\',til)
    system(\'color\',col)

a(64,25         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 13:21

    Let me clarify two points here :

    • Firstly non-default argument should not follow the default argument, it means you can't define (a = 'b',c) in function. The correct 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

    so first re-arrange your parameters

    • now the second thing is you have to define len1 when you are doing hgt=len1 the len1 argument is not defined when default values are saved, Python computes and saves default values when you define the function len1 is not defined, does not exist when this happens (it exists only when the function is executed)

    so second remove this "len1 = hgt" it's not allowed in python.

    keep in mind the difference between argument and parameters.

提交回复
热议问题