Using a decorator means that you "wrap" your other function, i.e. you call the decorator and then call the function from inside the decorator.
E.g.:
import functools
def outer(func):
@functools.wraps(func)
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner
Upon defining the function, the decorator will be called, returning the inner
function.
Whenever you call func
, you will in reality call inner
, which runs it's own code, including calling the original func
function.
So for your use case, you should be able to create decorators similar to:
def settings_menu(func):
@functools.wraps(func)
def inner(self, *args, **kwargs):
self.open_settings_menu()
self.current_view = "settings_menu"
return func(self, *args, **kwargs)
return inner
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…