Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the reasons why the Python function returns None by default

Shulou Source: shulou.com Published: 2022-06-02 22:26:09 09月15日 Update

I would like to share with you the reasons why the Python function returns None by default. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Python has a default practice, which is not available in many programming languages, and all functions have a return value, whether or not a return statement is written.

Before the official start, use the pass statement and... Object as an example, take a look at the function return value of Python:

Neither of the defined functions writes any return statements, but both can get a return value after the function is called. The execution effect is exactly the same as writing the return statement directly:

These four examples fall into two types: one that does not write return, but all have hidden return return values; and one that writes return and actually has a return value. In other words, the latter is consistent in semantics and behavior. although the former is missing in semantics, it has actual behavior and results; the behavior of the latter is dominant, while the former is recessive.

In the Zen of Python, there is a sentence that "explicit is better than Explicit is better than implicit". However, for the sake of brevity and convenience (Simple is better than complex), many behaviors in Python are actually implicit and leave some grammatical things to the interpreter to do.

Truth judgment is a hidden behavior, and so are the first two examples in this paper.

Using dis to look at the bytecode, you can see the changes behind it:

In this comparison diagram, we can see that the interpreter instructions of the above four functions are exactly the same!

Whether you write return or not, they execute the logic of return, and the default return value is None.

So the question is: why do functions in Python return None by default? How did it happen?

The answer lies in the interpreter. When the CPython interpreter executes to the last code block of the function, if it finds that there is no return value, it will actively add a Py_None value to return:

If the defined function does not return a value, the Python interpreter will (forcibly) inject a piece of return logic into us by default!

These are all the contents of the article "what are the reasons why the Python function returns None by default?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Function behavior interpreter interpretation statement implicit example actual article reason two content semantics logic same concise consistent exactly the same the above table not much Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Xiaomi Microsoft vpn