__metaclass__ and __author__ are already decorators

Paul Morrow pm_mon at yahoo.com
Sun Aug 22 02:46:37 CEST 2004


Leif K-Brooks wrote:
> Paul Morrow wrote:
> 
>> And I don't agree that this would be assigning new 'meaning' to an old 
>> syntax.  When a programmer creates a __xxx__ class attribute, he is 
>> not trying to create a normal 'class' attribute --- one the is 
>> inherited by instances of the class or that holds part of the 'state' 
>> of the class. Instead, he is trying to make a meta statement about the 
>> class (who wrote it, what happens during instance initialization, 
>> etc.).  In that sense, the meaning associated with defining __xxx__ 
>> attributes would stay the same.
> 
> 
> If we were talking about something like this:
> 
> def foo(self):
>     pass
> foo.__author__ = "Leif K-Brooks"
> 
> then you would be correct. But when syntax normally used for assigning 
> to a variable magically assigns to an attribute if the variable name 
> starts and ends with "__", then it's an existing syntax (variable 
> assignment) being used for something new (attribute assignment). Why 
> should this:
> 
> def foo(self):
>     bar = 42
> 
> mean anything different from this?
> 
> def foo(self):
>     __bar__ = 42

Because the intent of the two assignments is quite different.  Yes, 
their syntactic structure is similar, and what happens after they 
execute is similar, but the 'kind' of information the programmer is 
trying to represent is very different in the two cases.

In the first example, the programmer intends for bar to be a local 
variable, used in some way by foo.  In the second example, the 
programmer most definitely does *not* intend for __bar__ to be used by 
foo --- instead he is simply providing information *about* foo.  This 
better illustrates the difference.

     def circumference(diameter):
         __author__ = 'Paul Morrow'
         __version__ = '0.1'
         pi = 3.14
         return pi * diameter

So when we define an __xxx__ attribute inside of a function, we are 
trying to make a statement about the function --- we are not trying to 
create a local variable.  Therefore the Python system shouldn't create a 
local variable in this case; it should create a function variable 
instead (IMO).






More information about the Python-list mailing list