How does Python handle globals?

后端 未结 2 1870
悲哀的现实
悲哀的现实 2021-01-20 02:19

I\'ve come across some very odd handling of global variables in Python. I was hoping someone can explain and justify these surprises!

A) This code prints 10 as expec

相关标签:
2条回答
  • 2021-01-20 02:34

    The second instance rebinds a, so the compiler generates a local access for it. The other two only read a, and so normal global scope searching is performed.

    0 讨论(0)
  • 2021-01-20 02:45

    Basically, there are two rules:

    1. When you only read from a variable (inside a scope), Python will travel up the scope chain until it finds a variable of that name.
    2. When you write to a variable at least once, Python will always create the variable in the current scope.

    You can change the behavior of #2, however:

    • If you want a name to refer to a module-level variable, you can use global my_module_variable. When you now write to my_module_variable, Python will not create a local variable.
    • As of Python 3, you can also have a name refer to a variable in an enclosing scope: use nonlocal my_non_local_variable to have it refer to the variable in the nearest enclosing scope.

    Problems in your code

    B) You are using +=: You are trying to write to the variable. So rule number 2 is in effect, it will write to a variable in the current scope. However, it also has to read from it (print(a)), but the variable does not yet have a value, because you haven't written to it before. Python does not allow you to mix rule 1. and rule 2. in a function.

    If you wanted func() to work on the a = 10 variable, you could change your code like this:

    >>>> def func()
            global a
            print(a)
            a += 1
    >>>> a = 10
    >>>> func()
    10
    >>>> func()
    11
    
    0 讨论(0)
提交回复
热议问题