Tag Archives: divide into python

Global variable error: unboundlocalerror: local variable ‘l’ referenced before assignment

Conclusion:

    internal function, do not change the global variable can access the global variable internal function, modify the same global variable, python will think it is a local variable before internal function to modify a global variable with the same call variable names (such as print the sum), cause Unbound – LocalError

    The sum set in the program belongs to the global variable, and there is no definition of sum in the function. According to Python’s rules for accessing local and global variables, when searching for a variable,
    If the variable is not found in the local scope, then Python looks for the variable in the global variable. If it is not found, it throws an exception (NameError or Unbound-LocalError, depending on the Python version).

    if you have any references to external internal function function of the same variable or a global variable, and is modified for this variable. Python assumes that it is a local variable, and because there is no definition or assignment of sum in the function, it returns an error.

    from the following two procedures separate access or modify a global variable, is not an error ~

    # accessing global variables #! /usr/bin/python

    import
    sys

    sum
    =
    5

    def
    add
    (
    a
    =
    1
    .
    b
    =
    3
    ) :

        
    print
    a
    .
    b

        
    print
    sum
    #
    Just to visit
     

    add
    (
    4
    .
    8
    )

    print
    sum

    [
    root
    @rac3
    python
    ]
    # python local.py

    4
    8

    5

    5

    # modify a global variable of the same name
    Is considered a local variable

    #! /usr/bin/python

    import
    sys

    sum
    =
    5

    def
    add
    (
    a
    =
    1
    .
    b
    =
    3
    ) :

        
    print
    a
    .
    b

    #
    An inner function has a variable of the same name or a global variable that refers to an outer function, and changes are made to that variable. Python will think of it as a local variable

        
    sum
    =
    b
    +
    a
    #
    Modify inside the function

        
    print
    sum

    add
    (
    4
    .
    8
    )

    [
    root
    @rac3
    python
    ]
    # python local.py

    4 8

    12

    If the inner function has a variable of the same name or a global variable that refers to the outer function, and the variable is modified. Python assumes that it is a local variable, and because there is no definition or assignment of sum in the function, it returns an error

    #! /usr/bin/python

    import
    sys

    sum
    =
    5

    def
    add
    (
    a
    =
    1
    .
    b
    =
    3
    ) :

        
    print
    a
    .
    b

        
    print
    The sum # inner function refers to a variable of the same name and modifies that variable. Python thinks of it as a local variable. Because the sum variable was not defined before print here, an error will be reported. (It is recommended to compare with case 1, note: this is only before the above example: print sum)

        
    sum
    =
    b
    +
    a

        
    print
    sum

    add
    (
    4
    .
    8
    )

    print
    sum

    [
    root
    @rac3
    python
    ]
    # python local.py

    4
    8

    Traceback
    (
    most
    recent
    call
    last
    ) :

     
    File
    “local.py”
    .
    line
    10
    .
    in
    ?

       
    add
    (
    4
    .
    8
    )

     
    File
    “local.py”
    .
    line
    6
    .
    in
    add

       
    print
    sum

    UnboundLocalError
    :
    local
    variable
    ‘sum’
    referenced
    before
    assignment

    You can use the: global keyword when you access a global variable in your program and you want to change the value of the global variable. Declare the variable as a global in a function

    #! /usr/bin/python

    import
    sys

    sum
    =
    5

    print
    ‘Before changing: sum=’
    .
    sum

    def
    add
    (
    a
    =
    1
    .
    b
    =
    3
    ) :

        
    global
    sum

        
    print
    ‘add ‘:sum=’
    .
    sum

        
    sum
    =
    b
    +
    a

        
    print
    ‘function :sum=’
    .
    sum

    add
    (
    4
    .
    8
    )

    print
    ‘change sum=’
    .
    sum

    [
    root
    @rac3
    python
    ]
    # vim local.py

    Before you change:
    sum
    =
    5

    add
    In the function
    :
    sum
    =
    5

    When I change the function
    :
    sum
    =
     
    12

    After the change
    sum
    =
    12