Python foundation function scope

Time:2020-1-18

function

Function object

  • Function isFirst class object
  • Function name canCited
  • Function names can be treated asparameterUse
  • Function names can be treated asReturn valueUse
  • Function names can be treated asElement of container type

Function nesting

  • Nested CallCall function in function

  • Nesting definition: define function inside function

Namespace

Storage variable nameSpace, divided intoBuilt in namespaceGlobal namespaceLocal namespace

  • Built in namespace: Python, the name defined in advance, exists in the built-in namespace. For example: keywords, built-in methods

  • Global namespace: the name stored at the file level is the global namespace

    if / while / for Internal definitionThe name ofAfter executionAre stored in the global namespace

    #Example of if:
    var = 4
    if var > 5:
        Var = "var greater than 5"
    else:
        Var = "VaR is less than or equal to 5"
    Print (VaR) ා print result: VaR is less than or equal to 5
    
    #An example of while:
    num = 0
    while num <= 3:
        num += 1
    Print (Num): 4
    
    #For example
    for i in range(2):
        pass
    Print (I): 1
  • Local namespaceFunction internal definitionAll names of are stored in the built-in namespace of the current function

If you want to access a variable value, you must first access the corresponding namespace to get the name of the variable and the relationship between the name and the value memory address.

life cycle

  • Built in namespacePython interpreterEffective at startup and invalid at shutdown
  • Global namespaceCurrent Python fileEffective after execution
  • Local namespacefunctionEffective when called,Function body codeFailure at end

Lookup order

  • Local name: partialOverall situationBuilt-in function

  • Global name: overall situationBuilt-in

    If the built-in can’t be found again, an error will be reported

  • Internal functionName used, inDefinition phaseIt’s dead, andCall location independent

Scope of action

  • global scopeOverall situationThe name that can be called exists in the global scope.Built in and global namespaces belong here
  • Local scopelocalCallable names exist in the local scope.Local namespace belongs here

  • globalWill:Local namespaceIs declared asglobal variable

    Be careful:Global needs to be declared inside the function, if declared outside the function, the function still cannot operate

    #In this case, the value of X1 cannot be modified
    x1 = 1
    def func1():
        x1 = 2
    func1()
    Print (x1) - print result: 1
    
    #With global, you can change the value of x2
    x2 = 1
    def func2():
        Global x2: global variables
        x2 = 2
    func2()
    Print (x2) print result: 2
    
    #Global cannot operate on variables if declared outside a function
    global x3
    x3 = 1
    def func2():
        x3 = 2
    func3()
    Print (x3) print result: 1
  • nonlocalInLocal namespacestatementlocal variable

    The declared variable of nonlocal will look up the variable layer by layer from the outer function of the current function. If the outermost function is not found, an exception is thrown.

    def f3():
        x = 4
        def f1():
            y = 2
            def f2():
                nonlocal x
                x = 3
            Call F2 (), modify the value of name x in F1 scope
            Print (x) view X in the F1 scope. Print result: 3
        f1()
        Print (x) print result: 3
    f3()
    def f3():
        x = 4
        def f1():
            x = 2
            def f2():
                nonlocal x
                x = 3
            Call F2 (), modify the value of name x in F1 scope
            Print (x) view X in the F1 scope. Print result: 3
        f1()
        Print (x) print result: 4
    f3()

Recommended Today

RCAST 35: add type to currency

– font ALT: Simsun; MSO font charset: 134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:”Cambria Math”; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; Variable; Ose-1: 216301111; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /\* Style […]