Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to return literally nothing in python?

Tags:

python

I'm learning python and I was just wondering if I there was a way to write a code that does something like:

def f(x):
    if x>1:
       return(x)
    else:
        # don't return anything

I'm asking about the else part of the code. I need to not return anything if x<=1, returning None isn't acceptable.

like image 804
Kirca Avatar asked Mar 27 '13 01:03

Kirca


People also ask

Can a Python function not return anything?

A Python function will always have a return value. There is no notion of procedure or routine in Python. So, if you don't explicitly use a return value in a return statement, or if you totally omit the return statement, then Python will implicitly return a default value for you.

What does return nothing mean 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.

Should you return None in Python?

Either all return statements in a function should return an expression, or none of them should.

How do you create an empty function in Python?

In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement. We can use pass in empty while statement also.


4 Answers

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).

like image 156
Blckknght Avatar answered Oct 20 '22 18:10

Blckknght


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.

like image 30
Sinkingpoint Avatar answered Oct 20 '22 18:10

Sinkingpoint


There's 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('Let\'s 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.

like image 4
Hrishikesh Shukla Avatar answered Oct 20 '22 18:10

Hrishikesh Shukla


How about this?

def function(x):
     try:
         x = x+1
         return (x)
     except:
         return ('')
like image 3
Josef Avatar answered Oct 20 '22 18:10

Josef