Hi guys, Is there a difference between accessing decorators via '@' symbol and hard coding that ? esp when the function passed to the decorator is a recursive one?
See attachments result from deco_with@ is 1 1 0 1 1 0 1 1 0 result from deco_without@ is 1 1 2 3 5 8 13 21 34 1 1 0 3 5 0 13 21 0 Thanks , muru
#------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: Ratna # # Created: 20/03/2014 # Copyright: (c) Ratna 2014 # Licence: <your licence> #------------------------------------------------------------------------------- import math def isOddMy(func): def innerOdd(x): y = func(x) if math.fmod(y, 2) == 0 : return 0 else: if y is not None: return y else: return 0 return innerOdd @isOddMy def fib(n): #print n, if n == 0 : return 0 elif n == 1 : return 1 else: return fib(n-2) + fib(n-1) def main(): #oddFibi = isOdd(fib) #print [i for i in oddFibi(100)] for i in range(1,10): print fib(i), '''print fib1 = isOddMy(fib) for i in range(1,10): print fib1(i),''' if __name__ == '__main__': main()
#------------------------------------------------------------------------------- # Name: module1 # Purpose: # # Author: Ratna # # Created: 20/03/2014 # Copyright: (c) Ratna 2014 # Licence: <your licence> #------------------------------------------------------------------------------- import math def isOddMy(func): def innerOdd(x): y = func(x) if math.fmod(y, 2) == 0 : return 0 else: if y is not None: return y else: return 0 return innerOdd #@isOddMy def fib(n): #print n, if n == 0 : return 0 elif n == 1 : return 1 else: return fib(n-2) + fib(n-1) def main(): #oddFibi = isOdd(fib) #print [i for i in oddFibi(100)] for i in range(1,10): print fib(i), print fib1 = isOddMy(fib) for i in range(1,10): print fib1(i), if __name__ == '__main__': main()
-- https://mail.python.org/mailman/listinfo/python-list