[Tutor] returning None from function definition

Alan Gauld alan.gauld at yahoo.co.uk
Sat Oct 24 07:08:06 EDT 2020


On 24/10/2020 11:40, Alan Gauld via Tutor wrote:

> That's definitely inferior. False explicitly says why it is being
> returned. None requires the mental leap to remember that None is
> considered false in Python logic. Don't make your reader do
> more work than is necessary.

In fact its worse. it will cause the function to fail in
the case of explicit tests for True/false. Consider:

>>> def f(): return False
>>> if f() is False: print("Its false")

Its false
>>> def g(): return None
>>> if g() is False: print("Its false")

>>>

Note that an explicit test of the return value for
False (using is) fails when None is returned.
While I would not recommend using is in boolean tests
it does happen in real life. So returning none is not
only less readable it is also potentially buggy!


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list