Home > Net >  Setting a default value for a variable in function in python
Setting a default value for a variable in function in python

Time:04-13

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.

CodePudding user response:

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

  • Related