Internal/Embedded Functions
1、Definition: the use of keywords within the function of a functiondef
keyword defines a new function, and this new function is called an internal/inline function.
2, note: the entire body of the internal function in the scope of the external function, if there is no reference to the external function variables within the internal function, that is, access, then in addition to the external function in the body, in any other place can not be called on the internal function.
3、Internal functions can access variables in external functions, but they cannot use variables in external functions, i.e., they cannot attempt to change variables in external functions. However, it is possible to usenonlocal
The keyword modifies a variable of an internal function so that the internal function can access and use the variable of the external function.🌰1
def out_func(out_name): def in_func(in_name): print(out_name.title() + " " + in_name.title()) # Internal functions can access the variables of external functions, but they can't be used can't be changed print("this is user()") return in_func # Returns the internal function object, which is the internal function address. a = out_func("tom") print(a) print(type(a))
Output results:
With the output we can see that the call to the out_func function returns the address of a function of type
def out_func(out_name): def in_func(in_name): print(out_name.title() + " " + in_name.title()) # Internal functions can access the variables of external functions, but they can't be used can't be changed print("this is user()") return in_func # Returns the internal function object, which is the internal function address. a = out_func("tom") # The function object in_func is returned. a("jerry") # equivalent to in_func("jerry") out_func("tom")("jerry")
Output results:
a = out_func("tom")
statement is a call to an external function, and after execution the variable a is equivalent to thein_func
,a("jerry")
statement is equivalent toin_func("jerry")
。
out_func("tom")("jerry")
statements anda = out_func("tom")
、a("jerry")
The result is the same.
def out_func(out_name): def in_func(in_name): print(out_name.title() + " " + in_name.title()) # Internal functions can access the variables of external functions, but they can't be used can't be changed print("this is user()") return in_func # Returns the internal function object, which is the internal function address. in_func("test") # Local variables can't be used outside of external functions
Output results:
An internal function is also considered a local variable of the external function, so it is also unscoped outside the external function.
Tip:
title(): capitalize the first letter of the string from which it is called
This article on the basis of Python function nesting knowledge summarizes the article is introduced to this, more related Python function nesting content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!