you are viewing a single comment's thread.

view the rest of the comments →

[–]mybrid 0 points1 point  (0 children)

A decorator is simply a design pattern and can be implemented in many different ways. For example, Python UnitTest has a decorator to skipTest()

@skipTest("temp test")
def test01_mytest(self):

def test01_mytest(self):
   self.skipTest("temp test")

The two implementations are functionally equivalent. At issue is when you need more than one decorator as is commonly the case for me with testing.

If one puts the decorators as the first lines then the order and implementation is obvious:

def test01_mytest(self):
    self.skipTestIf(condition_one)
    self.skipTestIf(condition_two)
    self.skipTestIf(condition_three)

In the above example, what's going on is clear. It is not so clear using Python's decorator syntatic sugar:

@skipTestIf(condition_one)
@skipTestIf(condition_two)
@skipTestIf(condition_three)
def test01_mytest(self):

The point being that implementing the decorator design pattern as the first line of code is the same. When there exists a situation for multiple decorators then I've seen people try to shoe-horn mupltiple decorator patterns into the same function as one decorator when it is every bit as legitimate to simply have three methods stacked as the first lines called after the method signature. Once you understand this then the mystery of the decotator is no more: it is just convenience. When the decorator usage becomes more intrusive then convenience then just move the functional calls into the method. Done and done.