Are decorators really that different from metaclasses...

Paul Morrow pm_mon at yahoo.com
Sun Aug 29 12:10:13 EDT 2004


Bengt Richter wrote:
> On Sat, 28 Aug 2004 08:45:20 -0400, Paul Morrow <pm_mon at yahoo.com> wrote:
> [...]
> 
>>Cool!  Thanks!  Now I need to ponder why a function's docstring needs a 
>>different implementation than a class's docstring (i.e. why not just 
>>make it a straigtforward attribute of the function object).
>>
> 
> Disclaimer: I am just speculating from appearances accessible interactively,
> so don't take this as developer's documention of internals. I'm just reading
> between the lines, trying to recognize the underlying ideas ;-)
> 
> So, depending on your idea of 'straightforward' ISTM docstrings do look like
> 'straightforward' attributes in the way they behave:
> 

By straightforward here I mean stored directly in the object's __dict__. 
    It's such an obvious place to keep the docstring, and it's the 
expected place, since that's where it's (apparently) stored for classes 
and modules.

> So there is a difference between func and old-style-class docstring implementations.
> Were you using old style classes for your question?
> 

I'm hoping that it doesn't matter.  What I'm really trying to get at is 
a function's namespace --- a dictionary that maps function attribute 
names to objects --- so that the interpreter has a context in which to 
evaluate __xxx__ variables defined at the outermost level of a function 
def.

Remember, I'm asserting that when we define __xxx__ attributes inside of 
a function def, they are not intended to be local variables, used in the 
computation of the function's result.  Rather they are intended to be 
function meta data, and therefore should be evaluated (at function 
definition time, not function execution time), in the context of the 
function being defined.

For example, in the following function def, the comments specify (what I 
believe is) the author's intention behind each assignment.

    def circu(diameter):
       """Info about circu."""        # defines circu.__doc__
       __author__ = 'Paul Morrow'     # defines circu.__author__
       __features__ = memoized        # defines circu.__features__
       pi = 3.14                      # local variable definition
       return pi * diameter



> I don't know why I did this...
> 

You were trying to help us understand what's going on under the hood, 
and I really appreciate that research and explanation.  Thank you for 
taking such an interest in this issue.

Paul




More information about the Python-list mailing list