Python Tricks: Nothing to Return Here

Python Tricks: Nothing to Return Here

Python adds an implicit return None statement to the end of any function. Therefore, if a function doesn’t specify a return value, it returns None by default.

This means you can replace return None statements with bare return statements or even leave them out completely and still get the same result:

In [1]: def fool(value):
   ...:     if value:
   ...:         return value
   ...:     else:
   ...:         return None
In [3]: def foo2(value):
   ...:     """Bare return statement implies 'return None'"""
   ...:     if value:
   ...:         return value
   ...:     else:
   ...:         return
In [4]: def foo3(value):
   ...:     """Missing return statement implies 'return None'"""
   ...:     if value:
   ...:         return value

All three functions properly return None if you pass them a falsly value as the sole argument:

In [7]: type(fool(0))
Out[7]: NoneType
In [8]: type(foo2(0))
Out[8]: NoneType
In [9]: type(foo3(0))
Out[9]: NoneType

Now, when is it a good idea to use this feature in your own Python code?

My rule of thumb is that if a function doesn’t have a return value (other languages would call this a procedure), then I will leave out the return statement. Adding one would just be superfluous and confusing. An example for a procedure would just be Python’s built-in print function which is only called for its side-effects(printing text) and never for its return value.

Let’s take a function like Python’s built-in sum. It clearly has a logical return value, and typically sum wouldn’t get called only for its side-effects. Its purpose is to add a sequence of numbers together and then deliver the result. Now, if a function does have a return value from a logical point of view, then you need to decide whether to use an implicit return or not.

On the one hand, you could argue that omitting an explicit return None statement makes the code more concise and therefore easier to read and understand. Subjectively, you might also say it makes the code “prettier”.

On the other hand, it might surprise some programmers that Python behaves this way. When it comes to writing clean and maintainable code, surprising behavior is rarely a good sign.

For example, I’ve been using an “implicit return statement” in one of the code samples in an earlier revision of the book. I didn’t mention what I was doing- I just wanted a nice short code sample to explain some other feature in Python.

Eventually I started getting a steady stream of emails pointing me to " the missing return statement" in that code example. Python’s implicit return behavior was clearly not obvious to everybody and was a distraction in this case. I added a note to make it clear what was going on, and the emails stopped.

Don’t get me wrong-I love writing clean and “beautiful” code as much as anyone. And I also used to feel strongly that programmers should know the ins and outs of the language they’re working with.

But when you consider the maintenance impact of even such a simple misunderstanding. it might make sense to lean towards writing more explicit and clear code. After all, code is communication.

猜你喜欢

转载自blog.csdn.net/weixin_41905135/article/details/124893677