In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.