How do you detect if the optional parameter value was set?
Experience Level: Junior
Tags: Python
Answer
The standard way is to use a default value that the user would not be expected to pass, frequently the value None is used.
Example:
def do_something(name: str=None):
if name is None:
print("Name value not provided.")
Related Python job interview questions
What is a decorator in Python?
Python JuniorWhy would you return self from a class method?
Python JuniorHow do you define an optional parameter with no value in Python?
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 Junior