Namespaces in functions vs classes
ian.g.kelly at gmail.com
Tue Apr 19 18:59:13 CEST 2011
On Tue, Apr 19, 2011 at 10:31 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> Gerald Britton wrote:
>> I now understand the Python does
>> not consider a class definition as a separate namespace as it does for
>> function definitions. That is a helpful understanding.
> That is not correct. Classes are separate namespaces -- they just aren't
> automatically searched. The only namespaces that are automatically searched
> are local, non-local, global, and built-in.
The problem is that they are treated differently at run-time. A
function namespace is compiled into a collection of local variable
names and closures, and it is effectively immutable. This is
necessary in order to generate the correct bytecode for each type of
storage location. A class namespace ultimately becomes a dict, and it
is a key feature that these be mutable. However, this means that a
class namespace can't have closures.
More information about the Python-list