tweaking @decorator syntax

Dave Brueck dave at pythonapocrypha.com
Fri Aug 6 16:09:13 EDT 2004


Jeff Shannon wrote:

> Andrew Durdin wrote:
> 
>>
>> 5. In appearance it looks like a special kind of statement; and it is
>> not at all obvious to me that it is rather a kind of attribute or
>> modifier of the following function definition. I don't know of any
>> other construction in Python that performs a similar, unmarked (i.e.
>> non-explicit) modification of the following statement. So to me the
>> @syntax comes across as extremely un-Pythonic (by that I mean
>> radically inconsistent with the conventions of the rest of the
>> language). For this same reason, I dislike all the proposed syntaxes
>> which involve placing something before the "def" without a subsequent
>> indented block.
>>  
>>
> 
> Ah, yes!  Thank you for expressing the thing that's been really bugging 
> me about this syntax, but which I hadn't quite managed to put into 
> coherent words.  I kept thinking that having all that stuff "stacked up" 
> above the function def looked horribly ugly, but I couldn't quite 
> express why...  but I think you've hit on it exactly, there.
> 
> The fact that it adds punctuation to an otherwise low-punctuation 
> language compounds matters, and I too would rather see a keyword than a 
> punctuation mark (though I do also understand the reluctance to add new 
> keywords).  But really, it's the placement rather than the punctuation 
> that truly grates on my eyes.
> 
> Hm, just as an experiment...
> 
>    def f(x,y):
>        '''tests two ints for equality
>        '''
>        @accepts(int, int)
>        @returns(bool)
> 
>        return x == y
> 
> 
> I still don't care for the @, but this looks much less jarring to me.  
> At very least, the decorators are much more clearly associated with the 
> particular function.

Looks much less jarring for me, too. I think Andrew hit the nail on the 
head.

-Dave



More information about the Python-list mailing list