[docs] What are the rules for local and global variables in Python?

Julien Palard julien at palard.fr
Sun Jul 7 17:21:58 EDT 2019


Hi Will, thanks for reporting!

> The text reads that variables which are _only_ referenced *inside* a function will be global, surely this should be *outside*, which I think would also make the following sentence make more sense.

Stransgely enough, the documentation is right: there's a distinction between "referenced" and "assigned":

- If a name is only referenced in a function, it's a global.
- If a name is assigned in a function, it's a local (unless declared explicitly as global).

For example:

```
VAL = 42

def foo():
    print(VAL)

foo()
```

In this case, VAL is only referenced inside function foo, so it's global.

Bests,
-- 
Julien Palard
https://mdk.fr



More information about the docs mailing list