[Python-Dev] Decorator syntax (original) (raw)

Rob Cliffe rob.cliffe at btinternet.com
Wed Sep 2 12:15:39 CEST 2009


Hi All, This is my first post to python-dev so I will briefly introduce myself: My name is Rob Cliffe and I am a commercial programmer living in London, UK. I have some 30 years of programming experience but have only been using Python for a couple of years. First I want to say what a fantastic language Python is. It is THE best language for development in my opinion, and a joy to use.

My specific issue: I eventually got my head round decorator syntax and realised that what came after the '@' was (basically) a function that took a function as argument and returned a function as result. However it seems to me unPythonesque (i.e. an exception to Python's normal consistency) that the syntax of what follows the '@' should be restricted to either a single (function) identifier or a single (function) identifier with an argument list. The example I tried, which seems not an unreasonable sort of thing to do, was along the lines of:

def deco1(func): def deco2(func):

DecoList = [deco1, deco2]

@DecoList[0] # NO - CAUSES SYNTAX ERROR def foo(): pass

I am sure other guys have their own examples.

I am of course not the first person to raise this issue, and I see that Guido has a "gut feeling" against allowing a general expression after the '@'.

BUT - a general expression can be "smuggled in" very easily as a function argument:

def Identity(x): return x

@Identity(DecoList[0]) # THIS WORKS def foo(): pass

So - the syntax restriction seems not only inconsistent, but pointless; it doesn't forbid anything, but merely means we have to do it in a slightly convoluted (unPythonesque) way. So please, Guido, will you reconsider?

Best wishes Rob Cliffe -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20090902/f95e8e03/attachment.htm>



More information about the Python-Dev mailing list