Python pune talk decorators

317 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
317
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Python pune talk decorators

  1. 1. Decorators in PythonThey are just normal functionsPythonPune talk – 27th April, 2013
  2. 2. Properties of functions1.Functions / Methods are just objects inpython.All rules of normal objects work exactly - likeassignments etc. [https://gist.github.com/sidchilling/5480003]Can define one function inside anotherfunction. [https://gist.github.com/sidchilling/5480010]A function can return another function as well! [https://gist.github.com/sidchilling/5480019]
  3. 3. Properties of functions [contd.]5. If you can return a function, then you canpass a function to another function.[https://gist.github.com/sidchilling/5480024]
  4. 4. Decorators, finally!A decorator is a function which allows you toexecute code before and after a function youdecorate.What does that mean?
  5. 5. What does that mean?1. A decorator is just a function.2. Takes the function to decorate as argument.3. Makes a function inside itself - "the wrapper".4. The wrapper executes code before and aftercalling the function.5. The decorator just returns the wrapperfunction.[https://gist.github.com/sidchilling/5480027]
  6. 6. Properties of decorators1. Can I make more than one decorator todecorate a function? Sure, they are justfunctions! But order of decorators MATTERS. [https://gist.github.com/sidchilling/5480034]2. You can pass arguments to the decoratedfunction as well. Hint: The wrapper gets thearguments! [https://gist.github.com/sidchilling/5480041]3. You can use decorators with methods also -just take care of self. [https://gist.github.com/sidchilling/5480047]
  7. 7. Properties of decorators4. You can make a generic decorator as wellwhich will accept any number of arguments -use of *args and **kwargs come handy in here.[https://gist.github.com/sidchilling/5480054]
  8. 8. Arguments to decoratorsCan I pass arguments to decorators itself?Well... it is a little twisted as the only argumentto a decorator is the decorated function.But a function can return a function - so afunction can return a decorator as well. This,we call a "decorator maker" - which returns adecorator.[https://gist.github.com/sidchilling/5480066]
  9. 9. Arguments to the decoratorsIts simple now.We can obviously pass arguments to thedecorator maker which can then passarguments to the decorator and to the wrapperfunction as well.[https://gist.github.com/sidchilling/5480078]
  10. 10. Python must be concise!Basically, all we need is the wrapper function -the decorator function itself it mostly boiler platecode.Can I make it concise?There are libraries to do so. All you need todefine is the wrapper function.[https://gist.github.com/sidchilling/5480081]
  11. 11. Thanks!Siddharth SahaEmail: sidchilling@gmail.comPhone: +91 - 86987 - 89798

×