I am reading Engineering a Compiler (Cooper, Torczon) 3rd Edition, Chapter 5 Syntax-Driven Translation. It contains the paragraph:
“PYTHON does not provide type declarations. The first use of a name x in
a scope is its defining occurrence. If the first use assigns x a value, then it
binds x to a new local entity with its type defined by the assigned value. If
the first use refers to x’s value, then it binds x to a global entity; if no such
entity exists, then that defining use creates the entity. If the programmer
intends x to be global but needs to define it before using it, the programmer
can add a nonlocal declaration for the name, which ensures that x is in the
global scope.”
How exactly can an entity be created for an undefined variable? I would think that this would result in a NameError. Can somebody please explain what the bolded clause is referring to and please provide a simple example and explain how this is different from assigning a value to an extant global variable or a new local variable?