additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Also Known As Wrapper Motivation Sometimes we want to add responsibilities to individual objects, not to an entire class. A graphical user interface toolkit, for example, should let you add properties like borders or behaviors like scrolling to any user interface component.
functools import wraps def decorate_with_sword(func): @wraps(func) def func_wrapper(name): return '{} with a SWORD'.format(func(name)) return func_wrapper @decorate_with_sword def orc_attack(name): """function that hit's you! """ return "{} , I am, hitting you".format(name) print orc_attack('Rengar') print orc_attack.__doc__ print orc_attack.__name__ >>Rengar , I am, hitting you with a SWORD >>function that hits you >>orc_attack
# code here print cls() class UglyOrc(Orc): ugly = True Orc.get_instance() <Orc object at 0x1304da0> UglyOrc.get_instance() <UglyOrc object at 0x13057e0>