20. Multiple forms of python function return values

Insert picture description here

One thing to note is that after our Python function return, for example, it defines a local variable. When this function is exited, the reference count of this local variable will be reduced by 1. If the function scope is out of the C language, the local variable is directly Destroyed, and our Python does not have the concept of local variables, we are all references, so as long as it goes out of scope, the reference will be reduced by 1.
Then the question arises. Does the minus 1 disappear? After we return, can we not access it? Not really.
In Python's return, it will add 1 to it. First, it comes out. The return function is called to add 1 to it. Its original reference count is 1. When we return, we add 1 to this variable. Then It becomes 2, and it will be decremented by 1 when it exits. At this time, the reference count of re is 1, and the variable re can be accessed outside.
So you have to know this. Python functions can return directly, and all local variables inside can return, which is different from the C language.
Since Python is a reference internally, it will increase the reference count by 1, so this return is not so simple. You have to know this, why it can be used internally, it will increase the reference count by 1.
Why do you want to say this? When you make one yourself and pass the C language function to Python, you have to do the same thing. You apply for a space through the Python interface, then you have to add 1 to the reference count, otherwise it will happen. The scope of this function, this space is destroyed, so it must be increased by 1 to ensure access to the outside.
Insert picture description here

It can support multiple return values, which is equivalent to returning a tuple, which can be assigned to multiple values. It is not recommended to use this return method, as the readability will decrease, and some new features should not be used as much as possible, but you should know this This feature requires you to understand other people’s code.

Insert picture description here
Insert picture description here
The space generated by our local variables can be accessed externally, because return will increase its reference count by 1.

Insert picture description here

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/109282789