Is there a way to return literally nothing in python?

Is there a way to return literally nothing in python?

There is no such thing as returning nothing in Python. Every function returns some value (unless it raises an exception). If no explicit return statement is used, Python treats it as returning None.

So, you need to think about what is most appropriate for your function. Either you should return None (or some other sentinel value) and add appropriate logic to your calling code to detect this, or you should raise an exception (which the calling code can catch, if it wants to).

To literally return nothing use pass, which basically returns the value None if put in a function(Functions must return a value, so why not nothing). You can do this explicitly and return None yourself though.

So either:

if x>1:
    return(x)
else:
    pass

or

if x>1:
    return(x)
else:
    return None

will do the trick.

Is there a way to return literally nothing in python?

Theres nothing like returning nothing but what you are trying to do can be done by using an empty return statement. It returns a None.

You can see an example below:

if account in command:
    account()

def account():
    talkToMe(We need to verify your identity for this. Please cooperate.)
    talkToMe(May I know your account number please?)
    acc_number = myCommand()
    talkToMe(you said your account number is +acc_number+. Is it right?)
    confirmation = myCommand()
    if confirmation!=yes or correct or yeah:
        talkToMe(Lets try again!)
        account()
    else:
        talkToMe(please wait!)
    return

This will return nothing to calling function but will stop the execution and reach to the calling function.

Leave a Reply

Your email address will not be published. Required fields are marked *