Alternative decorator syntax decision

Paul Morrow pm_mon at yahoo.com
Fri Aug 20 21:34:27 EDT 2004


Christophe Cavalaria wrote:

> Paul Morrow wrote:
> 
> 
>>Note: This is *not* about implicit method typing... :-)
>>
>>Although I haven't seen it discussed anywhere, we already have a
>>decorator syntax (of sorts) that we use to annotate functions and
>>classes.  The " __var__ = " business.  Have we decided that it is
>>woefully insufficient?  I know that there is a preference for the
>>decorators to appear outside of the function def, but putting that
>>aside, this 'style' of decorating already has a precendent in python, so
>>it is probably as pythonic as you can get...
>>
>>     class Foo:
>>         __metaclass__ = M
>>         __automethods__ = True
>>         __author__ = 'Paul Morrow'
>>         __version__ = '0.1'
>>
>>     def baz(a,b,c):
>>         __synchronized__ = True
>>         __accepts__ = (int,int,int)
>>         __returns__ = int
>>         __author__ = 'Fred Flintstone'
>>
>>return a + b + c
>>
>>
>>What is the burning desire to abandon this style?
>>
>>Paul
> 
> 
> By what kind of black magic would setting the property __synchronized__ to
> True would make that function synchronized ? And how can I define my own
> decorators then ?

There would be two kinds of decorators.  Those that are simply 
informative (like __author__ or __version__), and those that have 
side-effects (like __metaclass__, __synchronized__, __automethods__, 
etc.).  Those with side-effects would be implemented as special functions...

     def synchronized(func, trueOrFalse):
         __decorator__ = True
         __author__ = 'Billy Batson'
         __version__ = '0.1'
         #
         # perform the synchronized operation on func...
         #


Here 'func' would be bound to baz (the function being decorated), and 
trueOrFalse would be bound to True (the value assigned to 
__synchronized__ in the definition of baz).

So we could define our own decorator functions and they would get called 
the same way that synchronized does.

     def simpleFunction(a, b, c):
         __myDecorator__ = (73, 'hello', [5,6,7])

         # ...

     def myDecorator(func, a, b, c):
         __decorator__ = True
         print func                  # 1.
         print a                     # 2.
         print b                     # 3.
         print c                     # 4.
         """Footnotes:
             1. prints <function simpleFunction at 0x0092B630>
             2. prints 73
             3. prints 'hello'
             4. prints [5,6,7]
         """


 >
 > And what about the other usage patterns for decorators
 > like the easy property getter/setter definition ?
 >

Sorry, not sure what you mean.  But would the above cover it?

Paul




More information about the Python-list mailing list