[Solved]: non-default argument follows default argument

Once in your life, you must have faced this error in Python

non-default argument follows default argument

Why?

In Python, normally you can’t define non-default arguments after default arguments in a function, method or class.

non-default…


This content originally appeared on DEV Community and was authored by Rajesh Joshi

Once in your life, you must have faced this error in Python

non-default argument follows default argument

Why?

In Python, normally you can't define non-default arguments after default arguments in a function, method or class.

  • non-default arguments
def greet(name):
    return f"Welcome {name}"
  • default arguments
def greet(name='Rajesh'):
    return f"Welcome {name}"

So, a combination of both of these looks something like this

def greet(name, place='Home'):
    return f"Welcome {name}, to {place}"

The above code is 100% correct. It works great.

But

def greet(name='Rajesh', place):
    return f"Welcome {name}, to {place}"

Executing this code will log, non-default argument follows default argument.

Solution?

The solution is very simple, just use * at 0th index in the definition.

Example

def greet(*, name='Rajesh', place):
    return f"Welcome {name}, to {place}"

This was introduced in Python 3.4

Don;t forget to pass required keyword arguments while calling the function, method or class

>>> greet(place='School')
Welcome Rajesh, to School

Thank you
Cheers


This content originally appeared on DEV Community and was authored by Rajesh Joshi


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Joshi | Sciencx (2021-09-29T03:59:09+00:00) [Solved]: non-default argument follows default argument. Retrieved from https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/

MLA
" » [Solved]: non-default argument follows default argument." Rajesh Joshi | Sciencx - Wednesday September 29, 2021, https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/
HARVARD
Rajesh Joshi | Sciencx Wednesday September 29, 2021 » [Solved]: non-default argument follows default argument., viewed ,<https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/>
VANCOUVER
Rajesh Joshi | Sciencx - » [Solved]: non-default argument follows default argument. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/
CHICAGO
" » [Solved]: non-default argument follows default argument." Rajesh Joshi | Sciencx - Accessed . https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/
IEEE
" » [Solved]: non-default argument follows default argument." Rajesh Joshi | Sciencx [Online]. Available: https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/. [Accessed: ]
rf:citation
» [Solved]: non-default argument follows default argument | Rajesh Joshi | Sciencx | https://www.scien.cx/2021/09/29/solved-non-default-argument-follows-default-argument/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.