r/learnpython • • 3d ago

Function without argument

Ive got a function that takes a dictionary as an argument.
Now Id like to program it so when the function is called without any argument, instead of an error message it prints something custom (a dictionary for example).

Any help is appreciated :D

1 Upvotes

16 comments sorted by

View all comments

34

u/atarivcs 3d ago

Assign a default value for the function parameter:

def myfunction(mydict=None):
    if mydict is None:
        # the caller did not pass an argument
        # so handle that however you want
    else:
        # the caller did pass an argument
        # so handle that however you want

7

u/Adbelhak_Unsicker 3d ago

Simple and avoids the mutable default argument issue, cant go wrong with this approach