Skip to content

Instantly share code, notes, and snippets.

@swuecho
Created July 9, 2012 18:20
Show Gist options
  • Select an option

  • Save swuecho/3078021 to your computer and use it in GitHub Desktop.

Select an option

Save swuecho/3078021 to your computer and use it in GitHub Desktop.
variables in python
a, b, c = (1, 2, 3)
print(a, b, c)
def test():
print(a)
print(b)
print(c) # (A)
c=1 # (B)
test()
"""for variable in python, you can see the variables in outer scope, but you
can not edit it."""
"""
Although scopes are determined statically, they are used dynamically. At any time during execution, there are at least three nested scopes whose namespaces are directly accessible:
the innermost scope, which is searched first, contains the local names
the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
the next-to-last scope contains the current module’s global names
the outermost scope (searched last) is the namespace containing built-in names
If a name is declared global, then all references and assignments go directly to the middle scope containing the module’s global names. Otherwise, all variables found outside of the innermost scope are read-only (an attempt to write to such a variable will simply create a new local variable in the innermost scope, leaving the identically named outer variable unchanged).
Usually, the local scope references the local names of the (textually) current function. Outside functions, the local scope references the same namespace as the global scope: the module’s namespace. Class definitions place yet another namespace in the local scope.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment