Steven D'Aprano <ste...@remove.this.cybersource.com.au> writes: > Modules are namespaces. So are packages. > > Classes and class instances are namespaces. > > Even function scopes are namespaces.
Steven implies it with his wording, but to make it explicit: When you have a module, package, class, or instance-of-a-class object, those objects are themselves namespaces. That is, the name used to refer to the object can be a component in a namespace reference:: import foo_package import bar_module class Parrot(object): widget = object() parrot = Parrot() # Use a package as a namespace. foo_package.spam_module.frobnicate() # Use a module as a namespace. bar_module.spangulate() # Use a class as a namespace. print Parrot.widget # Use an arbitrary instance as a namespace. parrot.state = "Restin'" When you have a function object, the “function scope” is not available in this way: you can't access the “inside” of the function from the outside via the function object. (The function object, like any other object, has a namespace, but that's not the *function scope* namespace.) > When you write: > > > n = None > > def spam(n): > print "spam" * n > > def ham(n): > print "ham" * n > > the n inside spam() and ham() and the global n are in different > namespaces, and so independent. Right. And neither of them is available from outside those functions (since the reference only exists while the function is executing). -- \ “Roll dice!” “Why?” “Shut up! I don't need your fucking | `\ *input*, I need you to roll dice!” —Luke Crane, demonstrating | _o__) his refined approach to play testing, 2009 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list