Python has a default practice that many programming languages don’t have — all of its functions return a value, whether or not you write a return statement.

This article is from the “Why Python” series. Before we begin, we use the pass statement and… As an example, see how Python functions can be created out of thin air:

As you can see, neither function we defined writes any return statement, but after the function call, we can get a return value.

They have exactly the same effect as writing a return statement directly:

These four examples fall into two types: one that does not write a return but has a hidden return value; One says return and actually returns a value.

That is to say, the latter is semantically and behaviorally consistent, while the former is semantically deficient but has actual behavior and results. The latter’s behavior is explicit, while the former is implicit.

Zen of Python has the phrase “Explicit is better than implicit.” However, for the sake of simplicity and convenience, In fact, much of Python’s behavior is implicit, leaving the interpreter to do some syntactic work.

The truth judgment in the previous installment was an implicit behavior, as were the previous two examples in this article.

Using dis to view the bytecode, you can see the little action behind it:

In this comparison, you can see that the interpreter instructions for the above four functions are exactly the same!

They execute return logic with or without return, and the default return value is None.

So, the question is: why do Python functions return None by default? How does it work?

The answer is in the interpreter. When the CPython interpreter reaches the last block of the function and finds no value returned, it automatically returns a Py_None value (from: compile.c) :

That is, if we define a function that does not return a value, the Python interpreter will (by default) inject us with a piece of return logic!

Do you find the interpreter’s ancillary services sweet, or do you dislike it too much?

It doesn’t seem to do much good, but it doesn’t seem to do any harm, either, right?

This, then, raises a new question: why does Python require functions to return values? Why doesn’t it provide a void keyword, as some languages do, and support defining empty functions with no return value?

We’ll find out about this in the next installment of the “Why Python” series.

If you found these questions illuminating, you’ll enjoy these articles:

1. Why does Python use indentation to divide code blocks?

2. Is Python’s indentation anti-human?

3. Why does Python use semicolons as statement terminators?

4. Why does Python have a main function? Why don’t I recommend main?

5. Why does Python recommend serpentine nomenclature?

6. Why doesn’t Python support the i++ increment syntax and the ++ operator?

7. Why can Python exchange two variables directly with a single statement: “a,b=b,a”?

8. Why does Python use # as a comment?

Why does Python have a pass statement?

10. Why does Python have a strange “…” Objects?

11. Why does Python support arbitrary truth determination?

This article is part of the “Why Python” series, which focuses on the syntax, design, and development of Python. The series tries to show the charm of Python by asking “why” questions. All posts will be archived on Github at github.com/chinesehuaz…