O'Reilly logo
  • Sri Harsha V thinks this is interesting:

Figure 17-1. The LEGB scope lookup rule. When a variable is referenced, Python searches for it in this order: in the local scope, in any enclosing functions’ local scopes, in the global scope, and finally in the built-in scope. The first occurrence wins. The place in your code where a variable is assigned usually determines its scope. In Python 3.X, nonlocal declarations can also force names to be mapped to enclosing function scopes, whether assigned or not.


Cover of Learning Python, 5th Edition


Just understand and memorize this figure. This covers almost everything about scoping in python.