Proposed improved decorator syntax

Hallvard B Furuseth h.b.furuseth at usit.uio.no
Sat Aug 7 20:51:12 EDT 2004


barnesc wrote:
> class foo:
>     def introduceNewFeature(self, someArgument, anotherArgument):
>         .decorate = [synchronized]
>         .accepts  = (int, int)
>         .author   = 'Chris King'
>         pass # whatever

I like it better than the current syntax, but I'm troubled by stuff
after the ':' which is executed before the function.
It _looks_ like it is executed inside the function.

Also, the scoping is a bit strange:

    def bar(self, baz):
        .decorate = [baz]
        pass # whatever

The decorator looks like it refers to the function parameter, but it
doesn't: the parameter has no value when the decorator is set.  Though I
guess a simple fix in this case is simply to forbid decorators to use
names that occur as function parameters.

Rob Williscroft wrote:

> But since we can have function's in function's and we already
> have "special" member functions (__init__ etc):
> 
> class Foo:
> 
>   def method( self ):
> 
>     def __decorate__( func ):
>       synchronized( func )
> 
>     __accepts__ = (int, int)
>     __author__ = "whoever"
> 
>     pass

Same problem, only worse.  The def statements of special member
functions are executed while the class body is executed, just like defs
of normal functions.  OTOH, your special functions inside functions must
be executed once before the function is called.

-- 
Hallvard



More information about the Python-list mailing list