What are decorators in Python?
Python decorators are just plain functions or other callables which take as single argument the function or class to be decorated. As such you can use many ordinary functions as decorators and vice versa.
def message(param1, param2): def wrapper(wrapped): class WrappedClass(wrapped): def __init__(self): self.param1 = param1 self.param2 = param2 super(WrappedClass, self).__init__() def get_message(self): return "message %s %s" % (self.param1, self.param2) return WrappedClass return wrapper @message("param1", "param2") class Pizza(object): def __init__(self): pass pizza_with_message = Pizza() print(pizza_with_message.get_message())
Sample output of above program.
message param1 param2
2019-07-09T01:57:02+05:30
2019-07-09T01:57:02+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution