[Python-Dev] Re: Call for defense of @decorators

Shalabh Chaturvedi shalabh at cafepy.com
Fri Aug 6 01:32:46 CEST 2004


I converted some code which makes good use decorators (an example follows):

    @paramnames(username='Username', password='Password')
    @webmethod(username=str, password=str)
    def login(self, username, password, return_to=None):
        request = current.request
        ...

Here is some usability feedback:

+ Easy to type. I found myself typing the function name before going up
one line and typing the decorators - @ decorator <ENTER> @ decorator
<ENTER>.

+ One line is one decorator - this preserves clarity while writing and
reading.

+ I look at the code and all decorators line up in a column just above the
function - very easily browsable and hard to miss. No searching or
scanning required.

+ Changing order or decorators (if it matters) is easy.

The only downside I found was:

- @ is a 'noisy' character and makes it look a little cluttered. Usually
def has an empty line before it but with decorators there is some funky
code sticking to it. Not a big deal, though.

Also, this symptom might get alleviated once editors know how to color
decorators nicely. An alternative character (as others have suggested)
might be good too. If so, I found '|' particularly nice :)

    |paramnames(username='Username', password='Password')
    |webmethod(username=str, password=str)
    def login(self, username, password, return_to=None):
        request = current.request
        ...

    |webmethod()
    def logout(self, return_to=None):
        current.request.session.user = None
        return current.request.redirect(return_to or self._q_location)

Appears accentuated, yet clean, specially for simple/single decorators.
Gives an appearance that the decorators are 'connected' to the function.

Conclusion: as a user I'm +1 on the @-decorator syntax, or any similar
syntax using an alternative character.

Cheers,
Shalabh



More information about the Python-Dev mailing list