Wandering Thoughts archives

2011-05-29

Getting the stages of the class namespace straight

Earlier I wrote that:

[...] metaclasses take effect after the class namespace has been set up (more or less).

This is of course false, but it's also true in the sense I was using it in that entry because there are two stages that the class namespace goes through.

The first stage is the namespace that exists when Python executes all of the code in the 'class A(B):' statement block. This code executes in an unnamed and normally inaccessible namespace that is the class namespace to be. This namespace is more or less a dictionary, which makes it like the module namespace and not at all like a function's namespace. Once the class's definition has finished executing, whatever is left in this namespace is passed to the metaclass for the metaclass to do whatever it wants.

(I don't think Python has a specific name for the class namespace in this state of the class being defined, but I could be wrong.)

The second stage is what we think of the class namespace; it is the namespace that is the actual class namespace once the class has been fully defined. Metaclasses can modify this namespace as it is in the process of being not so much created as used, so in this sense my original remark is false.

Note that although it's uncommon to do so, you can use 'del' on names in the class namespace as you're defining it. You can also get access to it with locals(), and in fact locals() will return a dictionary object that you can directly modify to modify the class namespace (although I would not count on this feature in non-CPython environments). For that matter, you can use del on a class's namespace even after it's been created.

(Since I have __slots__ on the mind, here's a trivia result. While you can delete the __slots__ attribute of a class after it has been created, this doesn't stop instances of the class being slots-based; as you might expect given how slots work, the slots-using nature of a class is set in stone the moment the class is created, or more specifically the moment the C blob representing the class is created and set up.)

python/ClassNamespaceStages written at 01:10:19; Add Comment


Page tools: See As Normal.
Search:
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.