[设计类时,我写了一个类成员函数作为装饰器
def switchWindow(self, win: str):
def actual_decorator(func):
self.browser.switch_to.window(self.windowsHandles[win])
def inner():
func()
return inner
return actual_decorator
装饰器用于在其他成员函数运行之前更改其装饰,但是,当我在这样的成员函数上使用装饰器时:
@switchWindow(win="crop")
def test_cropFunction(self):
pass
IDE引发了这样的错误:
@switchWindow(win="crop") TypeError: switchWindow() missing 1 required positional argument: 'self'
我对错误感到困惑,但是随后我通过在装饰器之前添加一个“ self”重写了装饰器,但这也是错误的:
@self.switchWindow(win="crop") NameError: name 'self' is not defined
有人可以帮我吗?
我将装饰器函数移出类,并进行如下更改:
def switchWindow(win: str):
def actual_decorator(func):
print(win)
def inner(self):
self.browser.switch_to.window(self.windowsHandles[win])
func(self)
return inner
return actual_decorator
现在可以使用了!