How do you define an optional parameter with no value in Python?
Experience Level: Junior
Tags: Python
Answer
To define an optional parameter with a no value, use None as the default value of the parameter in the method signature.
Example:
def do_something(name: str=None):
print("Name: " + name)
Related Python job interview questions
Why would you return self from a class method?
Python JuniorHow do you detect if the optional parameter value was set?
Python JuniorHow do you define an optional parameter with a default value in Python?
Python JuniorWhat is a data type timedelta good for in Python?
Python JuniorWhat is the datetime data type good for in Python?
Python Junior