[Python-Dev] Decorator syntax J2 (decorate..def), with implementation

Michael Sparks zathras at thwackety.com
Sat Aug 14 01:31:24 CEST 2004


Hi,


I suspect people are getting sick of the decorator discussion by now, so
I'll try to be brief, and useful.

Syntax option J2 from the wiki seems to have provoked an interesting
discussion[1] at least by a small subset of c.l.p, so I wondered how
difficult it would be to implement - this turned out to be relatively
simple [2].
   [1] 
       (implementation)

Example of the syntax... (I'm sure it's familiar...)

#Syntax J2 from http://www.python.org/moin/PythonDecorators
#Some random comment
#More random comment
decorate:
     staticmethod
     grammarrule('statement : expression')
     versioninfo("Added in 2.4")
     deprecated
     typeinfo(None)
def p_statement_expr(self, p):
     print p[1]

For comparison, this is intended as equivalent to:

#Current syntax in 2.4a2
#Some random comment
#More random comment
@staticmethod
@grammarrule('statement : expression')
@versioninfo("Added in 2.4")
@deprecated
@typeinfo(None)
def p_statement_expr(self, p):
     print p[1]

With the exact semantics and restrictions remaining the same - this is
just syntactic sugar.


This was my first time looking at the python source to see how easy it
would be to implement - so it's not perfect, and probably far from ideal.
My reasons I looking and giving it a go though were:

   * To me it looks much clearer - you can instantly see where the method
     (and decorator block) starts.
   * Lack of repetition of the decoration hint
   * It hints to a non-python programmer very clearly that this is
     something different. If they're aware of the decorator patterm,
     they will realise (perhaps) what it means, and if they don't they
     have something to google for.

I have read most of the arguments against this syntax, and if the decision
ends up to stay with @ or take a different syntax, I'm sure it'll work out
fine :) (This isn't my favourite syntax, my favourite has a keyword inside
the function suite, which I'm well aware is dead in the water!)

The post I made to c.l.p with a first attempt at an implementation (has
minor issues regarding scopes, but does work) can be found here:

   * [Python-Dev] Another 2.4 asyncore headache

	
  • Next message: [Python-Dev] Decorator syntax J2 (decorate..def), with implementation
  • Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

  • More information about the Python-Dev mailing list

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant