Python programming idea: Local functions can also be defined inside functions

Author : xuzhiping   2022-11-02 14:53:00 Browse: 1499
Category : Python

Abstract: Python not only supports global functions, but also supports the definition of functions in the function body. This kind of funct...

Python not only supports global functions, but also supports the definition of functions in the function body. This kind of function defined in the body of a function is called a local function.

Python

By default, local functions are hidden from the outside, Local functions can only be called inside the function body. Functions can also return local functions, so that the program uses local numbers in other scopes.

Sample code:local_func_test.py

# Defines a function that returns the calculation result of a local function
Def multi_math_func (type, value):
    # Define a local function that calculates the square
    Def square (n):
        Return n * n
    # Define a local function that calculates the cube
    Def cube (n):
        Return n * n * n
    # Define a local function for calculating factorial
    Def factorial (n):
        Result = 1
        For index in range (2, n + 1):
            Result * = index
        Return result
    # Call local function
    If type = = "square":
        Return square (value)
    Elif type = = "cube":
        Return cube (value)
    Else:
        Return factorial (value)
Print (multi_math_func ("square", 5)) # output 25
Print (multi_math_func ("cube", 7)) # output 343
Print (multi_math_func (", 4)) # output 24

A global function multi is defined in this code multi_math_func, in which three local functions are defined, while multi_math_func() function calls different local functions according to the parameter selection.

If multi_math_func function does not return a local function, so the local function can only be used in multi_math_func function, which is called internally, as shown in the above program.

In addition, there will be a case where multi_math_func function returns local functions, and the program uses variables to save the return value of the multi_math_func function, then the scope of these local functions will be expanded. Therefore, programs are free to call them as if they were global functions. Let's look at how to use local functions.

Sample code:return_func.py

# Defines a function that returns the calculation result of a local function
Def multi_math_func (type):
    # Define a local function that calculates the square
    Def square (n):
        Return n * n
    # Define a local function that calculates the cube
    Def cube (n):
        Return n * n * n
    # Define a local function for calculating factorial
    Def factorial (n):
        Result = 1
        For index in range (2, n + 1):
            Result * = index
        Return result
    # Call local function
    If type = = "square":
        Return square # returns the square function
    Elif type = = "cube":
        Return cube # returns the cube function
    Else:
        Return factorial # returns the factorial function
# Call get_math_func (), and the program returns a nested function
Math_func = multi_math_func ("cube") # get the cube function
Print (math_func (7)) # output 343
Math_func = multi_math_func ("square") # get the square function
Print (math_func (3)) # output 9
Math_func = multi_math_func ("other") # get the factorial function
Print (math_func (6)) # output 720

The multi_math_func function in this code returns 3 different functions according to the type parameter value, so the return value of the function is the function itself, and the return value of the function cannot be directly output, and the calculation result can only be obtained after calling it in the way of the function.

Label :
    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).