Are decorators really that different from metaclasses...

Steven Bethard steven.bethard at gmail.com
Wed Aug 25 00:08:47 EDT 2004


Paul Morrow <pm_mon <at> yahoo.com> writes:
> I believe that (virtually) all __xxx__ attributes have this metadata 
> aspect (semantics) to them.  When a programmer says
> 
>     def foo():
>         __author__ = 'Wilma Flintstone'
>         __version__ = '0.1'
> 
> She does not intend for __author__ nor __version__ to be local variables 
> of foo, used somehow by foo in the calculation of its return value.  To 
> her they are foo /metadata/ --- attributes of the foo object itself --- 
> as they contain information that *describes* foo.

I assume you mean that this is what you'd like a programmer to intend?  If I 
wrote that, I would intend (and expect) __author__ to be a local variable.  
I'm not saying that I couldn't be retrained.  I'm just saying that right now, 
I would not expect it to be otherwise.

> Likewise, when she defines __lt__, __getitem__, __init__, etc. as part 
> of a class, they will not typically be called by methods of the class or 
> users/consumers/clients of the class [*] the way that 'normal' 
> attributes will.  They contain meta info that describes a deeper level 
> of class behavior.

This seems a little misleading to me.  The only reason these methods are 
special is because they override operators (something like "<", "[]", and "()" 
respectively).  You could provide a class with /exactly/ the same 
functionality without ever implementing any of these methods.  (Well, minus 
__init__, but that was a special case in your discussion too.)  The only thing 
that implementing these methods does is allows your user to access these 
methods through an operator shorthand.  A simple example:

class Identity:
    def get(self, x):
        return x
    __getitem__ = get

How is __getitem__ any more "metadata" then get is?  They provide exactly the 
same functionality.  The __getitem__ declaration just allows you to access the 
get method using "[...]".

Are you trying to say that "metadata" is the same thing as "operator shortcut"?

Steve





More information about the Python-list mailing list