Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

None returned by function in Python

📅 2012-May-04 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ function, none, python ⬩ 📚 Archive

This will seem odd to programmers from other languages. In Python, a function that has no return statement or an exit path in the function that has no return statement actually returns None. So, a variable can be used assign the return value of such functions. This also means that one does not need to worry about every exit path in the function. In languages like C, such behaviour would be a compilation error.

def foo( x ):
    print( x )

y = foo( 10 )

if y is None:
    print( "foo returned None" ) # Printed
     print( "No, it did not!" )

Tried with: Python 3.2

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧