In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains why Python functions return None by default. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Why Python functions return None by default"!
Before we get started, let's use the pass statement and…object discussed earlier as examples to see how Python functions "come out of nothing":
As you can see, neither of the two functions we defined has any return statement written, but after the function is called, it can take a return value.
Their execution effect is exactly the same as writing a return statement directly:
These four examples fall into two categories: one that doesn't write return, but all have hidden return values; and one that writes return and actually has return values.
That is, the latter is semantically and behaviorally consistent; the former, though semantically absent, has actual actions and consequences; the latter's actions are explicit, while the former are implicit.
There is a line in Zen of Python that says "Explicit is better than implicit," but for simplicity and convenience (Simple is better than complex), there are actually many behaviors in Python that are implicit, leaving some things at the syntax level to the interpreter.
Truth judgments in the previous issue are implicit behavior, as are the first two examples in this article.
Using dis to view the bytecode, you can see the little action behind it:
In this comparison diagram, you can see that the interpreter instructions for the above four functions are exactly the same!
Whether return is written or not, they execute return logic, and the default return value is None.
So, the question comes: Why can Python's functions return None by default? How does it work?
The answer lies in the interpreter. When the CPython interpreter executes the last code block of the function, if it finds no return value, it will actively add a Py_None value to return (from: compile.c):
That is, if the defined function has no return value, the Python interpreter will (forcibly) inject us with return logic by default!
For this kind of complimentary service of the interpreter, do you think it is very considerate, or do you dislike it?
This approach does not seem to have much benefit, but it does not seem to have any harm.
At this point, I believe everyone has a deeper understanding of "why Python functions return None by default", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.