June 2017
Beginner
352 pages
8h 39m
English
Very occasionally we need to rebind a global name at module scope from within a function. Consider the following simple module:
count = 0def show_count(): print(count)def set_count(c): count = c
If we save this module in scopes.py, we can import it into the REPL for experimentation:
$ python3Python 3.5.0 (default, Nov 3 2015, 13:17:02)[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> from scopes import *>>> show_count()count = 0
When show_count() is called Python looks up the name count in the local namespace (L). It doesn't find it so looks in the next most outer namespace, in this case the global module namespace ...
Read now
Unlock full access