Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for no argument sent to a function in Python

So, within a function I would like to test for the presence of an argument the function is expecting. If there is an argument, do this, if no argument is sent form the calling programme and none is received into the function do that.

def do_something(calculate):
    if calculate == something expected:
        do this
    elif calculate not sent to function:
        do that 

So what would be the logical test/expression I'd need to check for to test if no argument was received into the function please?

like image 588
John Avatar asked Mar 13 '26 04:03

John


1 Answers

You could set the argument as None, and check if it is passed or not,

def do_something(calculate=None):
    if calculate is None:
        # do that
        return 'Whatever'
    if calculate == "something expected":
        # do this
    return 'Whateverelse'
like image 145
han solo Avatar answered Mar 14 '26 16:03

han solo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!