In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to nest Python functions", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to nest Python functions" article.
Internal / embedded function
1. Definition: use the keyword def keyword to define a new function in the body of a function. This new function is called internal / embedded function.
2. Note: the entire function body of the internal function is within the scope of the external function. If there is no reference to the external function variable in the internal function, that is, access, then except in the external function body, no internal function can be called anywhere else.
3. The internal function can access the variables of the external function, but can not use the variables in the external function, that is, you can not try to change the variables in the external function. However, you can use the nonlocal keyword to modify the variables of the internal function, so that the internal function can access and use the variables of the external function.
Def out_func (out_name): def in_func (in_name): print (out_name.title () + "+ in_name.title ()) # internal functions can access variables of external functions, but cannot be used to change print (" this is user () ") return in_func # returns internal function objects, that is, internal function addresses. A = out_func ("tom") print (a) print (type (a))
From the output, we can see that after calling the out_func function, the return is a function address and the type is a function.
Def out_func (out_name): def in_func (in_name): print (out_name.title () + "+ in_name.title ()) # internal functions can access variables of external functions, but cannot be used to change print (" this is user () ") return in_func # returns internal function objects, that is, internal function addresses. A = out_func ("tom") # returns the in_func function object a ("jerry") # equivalent to in_func ("jerry") out_func ("tom") ("jerry")
A = out_func ("tom") statement is a call to an external function, and after execution, the variable an is equivalent to in_func,a ("jerry") statement is equivalent to in_func ("jerry").
The result of the out_func ("tom") ("jerry") statement is the same as a = out_func ("tom") and a ("jerry").
Def out_func (out_name): def in_func (in_name): print (out_name.title () + "+ in_name.title ()) # internal functions can access variables of external functions, but cannot be used to change print (" this is user () ") return in_func # returns internal function objects, that is, internal function addresses. In_func ("test") # Local variables cannot be used outside of external functions
The internal function is also a local variable of the external function, so on the outside, it also has no scope.
Tip:
Title (): the first letter of the string that will be called is capitalized above is the content of this article about "how to nest Python functions". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please pay attention to 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.
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.