Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python Programming Essentials - M38 - Decorators

594 views

Published on

Slides from the training on the topic "Python Programming Essentials"

  • Be the first to comment

Python Programming Essentials - M38 - Decorators

  1. 1. http://www.skillbrew.com /SkillbrewTalent brewed by the industry itself Decorators Pavan Verma @YinYangPavan Founder, P3 InfoTech Solutions Pvt. Ltd. Python Programming Essentials
  2. 2. © SkillBrew http://skillbrew.com What is a decorator?  A decorator is a callable that takes a function as an argument and returns a replacement function  Useful to wrap new capabilities inside a function
  3. 3. © SkillBrew http://skillbrew.com Decorator examples @classmethod @staticmethod @property @login_required
  4. 4. © SkillBrew http://skillbrew.com Creating a decorator – our objective >>> @logger ... def foo1(x, y=1): ... return x * y >>> foo1(5, 4) Arguments were: (5, 4), {} 20 >>> foo1(1) Arguments were: (1,), {} 1
  5. 5. © SkillBrew http://skillbrew.com Nested functions >>> def outer(): ... x = 1 ... def inner(): ... print x # 1 ... inner() # 2 ... >>> outer() 1
  6. 6. © SkillBrew http://skillbrew.com Functions are first class objects # all objects in Python inherit from a common baseclass >>> issubclass(int, object) True >>> def foo(): ... pass >>> foo.__class__ <type 'function'> >>> issubclass(foo.__class__, object) True
  7. 7. © SkillBrew http://skillbrew.com Functions can be passed as arguments >>> def add(x, y): ... return x + y >>> def sub(x, y): ... return x - y >>> def apply(func, x, y): ... return func(x, y) >>> apply(add, 2, 1) 3 >>> apply(sub, 2, 1) 1
  8. 8. © SkillBrew http://skillbrew.com Functions can be returned from other functions >>> def outer(): ... def inner(): ... print "Inside inner" ... return inner ... >>> foo = outer() >>> foo <function inner at 0x...> >>> foo() Inside inner
  9. 9. © SkillBrew http://skillbrew.com Function closures >>> def outer(): ... x = 1 ... def inner(): ... print x ... return inner >>> foo = outer() >>> foo.func_closure (<cell at 0x...: int object at 0x...>,)
  10. 10. © SkillBrew http://skillbrew.com Function closures (2) >>> def outer(x): ... def inner(): ... print x # 1 ... return inner >>> print1 = outer(1) >>> print2 = outer(2) >>> print1() 1 >>> print2() 2
  11. 11. © SkillBrew http://skillbrew.com Simple decorator >>> def outer(some_func): ... def inner(): ... print "before some_func" ... ret = some_func() ... return ret + 1 ... return inner >>> def foo(): ... return 1 >>> decorated = outer(foo) >>> decorated() before some_func 2
  12. 12. © SkillBrew http://skillbrew.com Logger decorator >>> def logger(func): ... def inner(*args, **kwargs): ... print "Arguments were: %s, %s" % (args, kwargs) ... return func(*args, **kwargs) ... return inner
  13. 13. © SkillBrew http://skillbrew.com Logger decorator usage >>> @logger ... def foo1(x, y=1): ... return x * y >>> @logger ... def foo2(): ... return 2
  14. 14. © SkillBrew http://skillbrew.com Logger decorator usage (2) >>> foo1(5, 4) Arguments were: (5, 4), {} 20 >>> foo1(1) Arguments were: (1,), {} 1 >>> foo2() Arguments were: (), {} 2
  15. 15. © SkillBrew http://skillbrew.com References  http://simeonfranklin.com/blog/2012/jul/1/p ython-decorators-in-12-steps/

×