Are decorators really that different from metaclasses...

Paul Morrow pm_mon at
Sat Aug 28 00:39:51 CEST 2004

Anthony Baxter wrote:
> On Thu, 26 Aug 2004 16:09:42 -0400, Paul Morrow <pm_mon at> wrote:
>>Yes, it doesn't seem all that complex, although I'm not sure that
>>everyone reading this understands them and their subtleties.  The
>>following is an excerpt from
>>"A namespace is a mapping from names to objects. Most namespaces are
>>currently implemented as Python dictionaries, but that's normally not
>>noticeable in any way (except for performance), and it may change in the
>>future. Examples of namespaces are: the set of built-in names (functions
>>such as abs(), and built-in exception names); the global names in a
>>module; and the local names in a function invocation. In a sense the set
>>of attributes of an object also form a namespace."
>>When I talk about namespaces, I include all of the above, including the
>>sense mentioned in the last line.  So an object's attributes constitute
>>a namespace too.  Therefore __doc__, being an attribute of the function
>>object, is in the function object's /namespace/.  And note that this is
>>*not* a new namespace; it's been there all along.
> "In a sense" is the bit you're missing here. You can't just hand-wave
> and say that it's a namespace. It's *not* a namespace. If it was, you
> could do any of these:

No I didn't miss it (I read that bit).  In a sense, every object has 
it's own namespace.  So that means that every class, every module, every 
function has a namespace (in a sense).  The namespace (the mapping from 
names to objects) is stored in each object's special __dict__ attribute.

Ok, and this is certainly apparent with classes.

   >>> class Foo:
   ... 	"""docstring for Foo"""
   >>> Foo.__doc__
   'docstring for Foo'
   >>> Foo.__dict__.keys()
   ['__module__', '__doc__']

And because of the parallels between the above class definition of Foo 
and the following function definition of baz, I would expect the same 

   >>> def baz():
   ...    """docstring of baz"""
   >>> baz.__doc__
   'docstring of baz'
   >>> baz.__dict__.keys()

Say what?  Why didn't the Python system put baz's docstring into it's 
namespace (__dict__)?   And where did it put it?

I would like to understand the answers to these questions.  Can you 
answer them (will you)?  If not, can you please point me at something 
that documents what's going on here?



More information about the Python-list mailing list