[Python-Dev] Re: Call for defense of @decorators (original) (raw)

Guido van Rossum guido at python.org
Sun Aug 8 17:25:19 CEST 2004


Having just gone through the wiki page again, I think the last example under A (quoted below) shows up very plainly the only really serious problem with the before-def decorator syntax:

class TestDecorators(unittest.TestCase): ... def testdotted(self): decorators = MiscDecorators() @decorators.author('Cleese') def foo(): return 42 self.assertEqual(foo(), 42) self.assertEqual(foo.author, 'Cleese') Suppose that you were not familiar enough with python to have heard of decorators: looking at this code, would you have any idea that the @ statement (potentially) fundamentally affects the behaviour of the foo() function? Is there anything except their proximity to suggest that they are interdependent? The only implication that they might be is the second assertEqual call, and that is by no means obvious.

This an example of bad formatting. Tests often do that because their purpose is to test odd corners of functionality, not serve as sample code. Given that in typical example code there will always be a blank line separating the decorator from anything previous (and separating the body from what follows!) the connection between the decorator and the def is pretty clear.

Also note that arguments based on partial knowledge of Python can be used as an argument against pretty much anything (just tweak the assumptions a bit), and hence have little value.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list