Python ProgrammingPython Programming

Decorator for both class methods and functions in Python

Function decorators can also be used to manage function objects and Class decorators can also be used to manage class objects directly.
def deco(func):
    def inner(*args):
        print('Decorator: args={}'.format(args))
        func(*args)
    return inner
 
 
class Class:
    @deco
    def class_method(self, param):
        print('Parameter is {}'.format(param))
 
 
@deco
def static_function(a, b, c):
    print('{} {} {}'.format(a, b, c))
 
 
Class().class_method(25000)
static_function('Australia', 'Germany', 'London')
Sample output of above program.
Decorator: args=(<__main__.Class object at 0x0000000001262278>, 25000)
Parameter is 25000
Decorator: args=('Australia', 'Germany', 'London')
Australia Germany London