2

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?

3
  • If your function signature mentions argument and your function call doesnt provide the exact number of arguments, python throws an error. Commented Mar 6, 2019 at 14:15
  • 1
    Your function can't be called without arguments, passing in a value for calculate is mandatory. Commented Mar 6, 2019 at 14:19
  • According to a coding exercise on exercise.io - it asks for a certain course of action if no argument is given into the function. So... err... Commented Mar 6, 2019 at 14:54

2 Answers 2

9

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'
Sign up to request clarification or add additional context in comments.

7 Comments

Have tried but it still doesn't do what I wish. Specifically I am running an exercise from exercism.io where I have to write some code for Two-fer or 2-fer which is short for two for one. One for you and one for me. "One for X, one for me." When X is a name or "you". If the given name is "Alice", the result should be "One for Alice, one for me." If no name is given, the result should be "One for you, one for me." The code I have written is failing on the 'If no name is given" part.
Running this code in Pytest gives an error def two_fer(name=None): if name == 'Alice' or name == 'Bob': return ('One for ' + str(name) + ", one for me.") elif name == False: return ('One for you, one for me.')
__________________________________________________ TwoFerTest.test_no_name_given __________________________________________________ self = <two_fer_test.TwoFerTest testMethod=test_no_name_given> def test_no_name_given(self): > self.assertEqual(two_fer(), 'One for you, one for me.') E AssertionError: None != 'One for you, one for me.' two_fer_test.py:10: AssertionError =============================================== 1 failed, 2 passed in 0.07 seconds ================================================
Sorry about the lack of formatting, I am not sure how to format code in the comment replies.
def two_fer(name=None). Please note the name='None'
|
0

Use a sentinel.

SENTINEL = object()
def do_something(calculate=SENTINEL):
    if calculate is SENTINEL:
        # do that
        return 'Whatever'
    if calculate == "something expected":
        # do this
    return 'Whateverelse'

This way you can also distinguish between sending None to the function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.