'Setting a default value for a variable in function in python

Here I have a function

def myfunc(some_text, argument):
    print(some_text)
    if argument == True:
        print("Argument is True")

But I want it make it so that if I dont type anything for argument, it should just assume it as False instead of giving me an error saying that its missing one argument.



Solution 1:[1]

You should change your function signature to look like the following:

def myfunc(some_text, argument=False):
    print(some_text)
    if argument == True:
        print("Argument is True")

When you set a function parameter using '=', it assumes this value unless something different is passed in

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kiran