Python returns null function | Example code

If you want to return a null function in Python then use the None keyword in the returns statement.

Example Python return null (None).

def NoOne(x):
    print(x)
    return None


print(NoOne("Function Called"))

Output:

Python returns null function

Note: There is no such term as “return null” 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

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.

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

OR

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

Do comment if you have any doubts and suggestions on this Python return topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.