我需要一個裝飾器,它需要一個參數,並且該方法應該被允許運行(基於一些簡單的邏輯)或引發異常。但我不知道我該如何寫這個裝飾器。讓我們的方法運行或引發異常的Python裝飾器
class One(obj):
trend = "trend"
@myDecorator(self.trend)
def click_button(self):
clickable_element = self.driver.find_element_by_id(self.trend)
clickable_element.click()
return self
class Two(obj):
map = "map"
@myDecorator(self.map)
def click_button(self):
clickable_element = self.driver.find_element_by_id(self.map)
clickable_element.click()
return self
的邏輯應該是這樣的:
def my Decorator(arg):
if arg:
"run the method"
else:
raise "Exception"