In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use two nesting methods of python function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Cross nesting
The way of cross-nesting is to call the nesting method of the same or upper-level function in this function:
Def func (foo): print (1) foo () print (3) def a (): print (1) b = func (a) print (b)
The result of the output is:
one
one
three
None
First, the program runs the top code in the Python file. The functions func and an are first opened up for memory storage, but will not be executed. When the program comes to the assignment operation, it first executes the code to the right of the equal sign. The function func is called and the function an is passed to func as an argument. The func function is executed, also from top to bottom, first printing out 1, and then calling the parameter foo.
It is important to note that foo is a formal parameter and the argument is a. Calling foo at this point means calling the function a. The function an is called and another 1 is printed out. Function a finishes running, returns to function func, continues to execute the following code, and prints out 3. Finally, the function returns None by default and assigns a value to b. The program runs to an end.
Take a look at the following code:
Def func (): print (1) print ("it's too hard for me") print (2) def foo (b): print (3) ss = b () print (ss) print (4) def f (aMaginb): a (b) f (foo,func)
The result of the output is:
three
one
It's too hard for me.
two
None
four
As above, load all the functions into the newly opened memory space, but do not execute them. In the end, the f function is called, and the foo and func functions are passed to the function f as arguments. In function f, the foo function is called with an argument to the func function. Go to the foo function and print 3 first. To the assignment statement, first execute the code to the right of the equal sign, and the function func is called.
In the function func, print three things: 1, I'm too difficult, and 2. The default return value of the function is None, which is assigned to ss. Printing ss is printing None. Finally print 4, then return to the function f, and then return to global space. Execution is over.
Loop function
A loopback function is a nested method that calls a subordinate function in a function:
Def func (a): def foo (b): print (b) return foo (a) # execute the function call a = func (4) 7) print (a)
The result of the output is:
4 7
None
The function is still stored in the newly opened space and will not be called. When you run to the assignment statement, execute the code to the right of the equal sign and pass the two numbers to the function func. Inside the function, it is still necessary to open up a space to put the function foo in. Running to return does not immediately terminate the function, but instead runs the code that follows return first. The foo function is called, and the values passed in are 4 and 7, and then printed.
It should be noted that the shape of the function foo participates in the parameter of the function func is the same, so don't be confused. This is not recommended when writing code every day. After printing out 4 and 7, run to the last line of the function, which returns None by default. And then assign it to an and print it out.
This is the end of the content of "how to use the two nesting methods of the python function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.