DEV Community

Discussion on: GOF inspired python decorators

Collapse
 
swarupkm profile image
Swarup Kumar Mahapatra
def multiply(arg1, arg2):
  arg1 * arg2

def add(arg1, arg2):
  arg1 + arg2


operator_function_map = {
'*' : multiply,
'+':  add
}

def calc(operator, arg1 ,arg2):
  operator_function_map[operator](arg1,arg2)

calc('*' , 1 , 2 ) 

Kind of, does the job ?

Collapse
 
srininara profile image
Srinivas

Hi Swarup,
That is true for this example. Please remember this is a toy example to illustrate the concepts which I was trying to explain. The idea of these approaches is the ability we have to influence behavior from outside using decorators instead of having centralized control. Even the last example that I have provided works like a plug-in setup which allows for extension from outside. Hope that clarifies