Python: Why is global needed only on assignment and not on reads?

前端 未结 4 1133
一向
一向 2020-12-09 15:00

If a function needs to modify a variable declared in global scope, it need to use the global declaration. However, if the function just needs to read a global variable it ca

4条回答
  •  长情又很酷
    2020-12-09 15:21

    With nested scopes, the variable lookups are easy. They occur in a chain starting with locals, through enclosing defs, to module globals, and then builtins. The rule is the first match found wins. Accordingly, you don't need a "global" declaration for lookups.

    In contrast, with writes you need to specify which scope to write to. There is otherwise no way to determine whether "x = 10" in function would mean "write to a local namespace" or "write to a global namespace."

    Executive summary, with write you have a choice of namespace, but with lookups the first-found rule suffices. Hope this helps :-)

    Edit: Yes, it is this way "because the BDFL said so", but it isn't unusual in other languages without type declarations to have a first-found rule for lookups and to only require a modifier for nonlocal writes. When you think about it, those two rules lead to very clean code since the scope modifiers are only needed in the least common case (nonlocal writes).

提交回复
热议问题