Python decorator with and without arguments
def someDecorator(arg=None): def decorator(func): def wrapper(*a, **ka): if not callable(arg): print(arg) return func(*a, **ka) else: return 'xxxxx' return wrapper if callable(arg): return decorator(arg) # return 'wrapper' else: return decorator # ... or 'decorator' @someDecorator(arg=1) def my_func(): print('my_func') @someDecorator def my_func1(): print('my_func1') if __name__ == "__main__": my_func() my_func1()
Output
1 my_func