Return value + scope of function, closure

  1. Return value of function

    '''
      def Function name ([parameter]):
          print('. . . . ')
    
      //In all functions, if there is no keyword return, the default return value is None
    
    //Define a function, find the sum of even numbers in the range of 1-n (the parameter is n), and return the sum after operation
    return This keyword has another function: end method call
    '''
    def f1(n):
        he = 0
        for i in range(1, n + 1):
            if i % 2 == 0:
                he += i
    
        return he
    
    # Calling function
    s = f1(1)
    print(s)
    
    s = f1(10)
    print(s)
    '''
     Return value followed by multiple
     Conclusion:
     1. Use the return keyword to return something in the function
     2. return keyword has another function: end method
     3. The return keyword can be followed by multiple return results, which can be put into a tuple
    
     As long as there is a return value, you must receive the return result when calling the function
    '''
    
    
    def f1():
        return 1, 2, 3, 4  # (1,2,3)
    
    
    result = f1()
    print(result)  # (1,2,3,4)

     

  2. Scope of function

    '''
    Scope of function:
    #Global variable: defined at the outer layer of a function, it is called a global variable. Feature: any function can reference a global variable
      Local variable: defines a variable in a function, which is called a local variable
      Rule of function reference variable: first, find out whether the variable exists in the function itself. If it exists, use its own first. If it exists, use its own first
      If this variable does not exist, it will look out (global variable)
    
      If the function wants to modify the value of the global variable, you can use: global global variable name in the function
     Scope of function: LEGB
     Local: local
     Embedding: nested
     global: global
     builtin: built in
    
    '''
    #Global variable
    m = 80
     name = 'Zhang San'
    def f0():
        global m
        m += 10
        print(name, m)
    
    
    def f2():
        print('value of print M: {} '. format(m))
    
    
    def f1():
        #Local variable
        m = 100
        m -= 50
        print({} value of m after the end of '{}' operation: {} '. format(name, m))
    
    
    print(f1)
    f1()
    print(m)
    f0()
    f2()
    '''
     //The global variable of variable type does not need to be added when it is modified in the function
     //When the global variable of immutable type is modified in the function, global must be added
    global:  Declare the
      globals(): Get global variables saved in a dictionary
      nonlocal: Inner function to modify variables of outer function, you need to add nonlocal
      locals(): It represents the variables in a function and stores them in the form of a dictionary
    '''
    # global variable
    number = 10
    def func():
        # number = 100  # local variable
        global number
        print(number)  # Unresolved reference 'number'
        number = 9  # Equivalent to modifying the global variable number=10
        # number += 5
        print(number)
    func()
    print(number)
    # global variable
    books = ['The Dream of Red Mansion', 'Journey to the West', 'Water Margin', 'Romance of the Three Kingdoms']  # Variable type
    number = 10  # Immutable type
    def func():
        # books = []
        books.append('Chopping Dragon')
        print(books)
        global number
        number += 8
    func()
    print(books)
    print(number)
    # n = 10
    # list1 = [1, 2, 3]
    # def f1():
    #     global n
    #     n += 1
    #     # r = globals()
    #     # print('global: 'inside function, R)
    # m = 10
    # def f2():
    #     m = 1
    #
    #     def f3():
    #         nonlocal m
    #         m += 1
    #         print('m value: {} '. format(m))
    #
    #     f3()
    #     print('------->m:',m)
    #     r = locals()
    #     print(r)
    # # Calling function
    # print('front - > ', n)
    # f1()
    # print('post - > ', n)
    # r = globals()  # What are the variables loaded into memory in the current scope at runtime
    # print(r)
    # f2()
    # def f4():
    #     a = 1
    #     b = 2
    #     c = 3
    #
    #     def f0():
    #         print('hahahaha ')
    #
    #     r = locals()
    #     print(r)
    #
    # f4()
    
    # def max():
    #     pass
    
    def f5():
        # def max(n):
        #     pass
        list1 = [1, 3, 6, 8, 9]
        m = max(list1)
        return m
    
    r = f5()
    print(r)
    
    '''
    //Function nesting:
        //Define another function in one function, nesting of functions
        1. Normal values can be used as return values return come out
        2. Internal function is also a variable (special variable),So the inner function can also be used as a return value return Return out
    '''
    
    
    # Defining functions: outer functions
    def A():
        x = 10
        print('------------>1')
    
        # Define function inner layer function
        def B():
            print('----->BBBBB')
    
        print('------------->2')
    
        x += 10
        return B
    
    
    # Calling function
    print(A)
    r = A()
    print(r)
    # Get the final x value
    r()

     

  3. closure

    '''
     //Closure:
      1. Another function is defined inside one function
      2. Inner function references variables of outer function
      3. The return value is an inner function
    '''
    
    
    def outer_func(n):
        m = 10
        x = 100
    
        # Defining internal functions
        def inner_func():
            r = m + n
            print('And is:{}'.format(r))
    
        # inner_func()
    
        return inner_func
    
    
    print(outer_func)  # <function outer_func at 0x0000000001E051E0>
    
    result = outer_func(5)
    print(result)
    
    result()
    result()
    result()
    

     

Published 10 original articles, won praise 2, visited 71
Private letter follow

Posted by bjoerndalen on Sat, 11 Jan 2020 00:04:23 -0800