Create decorators with parameters in Python
Both function and class decorators can also seem to take arguments, although really these arguments are passed to a callable that in effect returns the decorator, which in turn returns a callable.
class Profile(object): def __init__(self, flag): self.flag = flag def __call__(self, original_func): decorator_self = self def wrappee(*args, **kwargs): print('Skills Before Joining: ', decorator_self.flag) original_func(*args, **kwargs) print('Skills After Joining: ', decorator_self.flag) return wrappee @Profile('Php, Java, Python, Go') def employee(name, age): print('Employee: ', name, age) employee('John Doe', 28)
Sample output of above program.
Skills Before Joining: Php, Java, Python, Go Employee: John Doe 28 Skills After Joining: Php, Java, Python, Go
2019-07-10T01:34:12+05:30
2019-07-10T01:34:12+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution