top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is decorator in python and when it is used and useful ?

0 votes
311 views
What is decorator in python and when it is used and useful ?
posted May 27, 2017 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Hi,

Say you have function or class named XXX and you want to modify the functionality or property of XXX without modifying XXX then you can use decorator to do this.

E.g

def decorator_fun(old_function) :
*****def extended_old_function(arg1,arg2):
********return 2 * old_function(arg1,arg2);
******return extended_old_function;
def multiplier(arg1,arg2) :
*******return arg1*arg2;

double_multiplier = decorator_fun(multiplier);
print double_multiplier(10,20)

in the above example multiplier is a function which functionality is extended using decorator_fun.

answer May 29, 2017 by Jaganathan
...