global var becomes local --UnboundLocalError: local variable referenced before assignment
Question: I initialized `rate` as a global var:

    
    
    import os, sys
    rate=30
    
    def foo():
        print('#########rate:', rate)
        if False:
            rate=int(sys.argv[2])
    
    
    foo()
    

but when running the script, I get the following error:

    
    
    Traceback (most recent call last):
      Line 10, in <module>
        foo()
      Line 5, in foo
        print('#########rate:', rate)
    UnboundLocalError: local variable 'rate' referenced before assignment
    

although the `if False: rate=int(sys.argv[2])` statement is not executed, it
seems has some influence, is there some python rules explains this?


Answer: You should declare `rate` as global:

    
    
    import os, sys
    rate=30
    
    def foo():
        global rate # <----
        print('#########rate:', rate)
        if False:
            rate=int(sys.argv[2])
    
    
    foo()
    

If there is assignment to a varaint (without global declaration), it is
treated as local variable.

