Monday, 4 January 2016

Decorators, brief view

#Create the decorator

def int2float_decorator(intf):
  def fdec(a,b):
     return float(intf(a,b))
  return fdec

#Use it at function declaration

@int2float_decorator
def int_sum(a,b):
  return int(a)+int(b)

#Or use it afterwards, decorating the method permanently

def int_sum(a,b):
  return int(a)+int(b)

float_sum = int2float_decorator(int_sum)

c = float_sum(3,4)

#Or at function call

c = int2float_decorator(int_sum)(3,4)

No comments:

Post a Comment