Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading