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.")

Python for beginners
Python for beginners

Are you learning Python ? Try our test we designed to help you progress faster.

Test yourself