Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SyntaxError: non-default argument follows default argument

Tags:

python

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,"hi","0b") input() 

When I run this, it rejects "def a(..." and highlights "(" in red. I have no clue why.

like image 363
YoungCoder5 Avatar asked Jul 13 '14 03:07

YoungCoder5


People also ask

Can non-default argument follow default argument?

The error “SyntaxError: non-default argument follows default argument” occurs when you specify a default argument before a non-default argument. To solve this error, ensure that you arrange all arguments in your function definition such that the default arguments come after the non-default arguments.

How do you fix positional arguments follows keyword arguments?

Since Python interprets positional arguments in the order in which they appear first and then followed by the keyword arguments as next. We can resolve the SyntaxError by providing all the positional arguments first, followed by the keyword arguments at last.

What is a default argument in Python?

Default arguments in Python functions are those arguments that take default values if no explicit values are passed to these arguments from the function call.

Does Python support default arguments?

Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.


2 Answers

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.

like image 110
Aaditya Ura Avatar answered Sep 25 '22 05:09

Aaditya Ura


As the error message says, non-default argument til should not follow default argument hgt.

Changing order of parameters (function call also be adjusted accordingly) or making hgt non-default parameter will solve your problem.

def a(len1, hgt=len1, til, col=0): 

->

def a(len1, hgt, til, col=0): 

UPDATE

Another issue that is hidden by the SyntaxError.

os.system accepts only one string parameter.

def a(len1, hgt, til, col=0):     system('mode con cols=%s lines=%s' % (len1, hgt))     system('title %s' % til)     system('color %s' % col) 
like image 33
falsetru Avatar answered Sep 21 '22 05:09

falsetru