Introduction to Python decorators

1,305 views
1,114 views

Published on

Presentation held at the Amsterdam Python Meetup Group

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
1,305
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to Python decorators

  1. 1. Python Decorators Introduction to Python decorators Rik van Achterberg | 2013-12-04
  2. 2. You might have seen them
  3. 3. Some theory ● Introduced in Python 2.4. ● Decorators can be used to implement the decorator pattern. ● Decorators are similar to annotations in Java/C#. ● When compared to C, decorators are comparable to preprocessor macros. ● Decorators are possible thanks to functions, like everything else, being objects.
  4. 4. Decorators replace functions Function being replaced by class Function being replaced by (inner) function
  5. 5. Decorators are just syntactic sugar ● A decorator is a function or class that takes a function as an argument and returns a replacement callable. ● The ‘@object’ syntax above functions is not magic; ● It just provides a clean notation.
  6. 6. How decorators are being called Identical behaviour with and without decorators Adding ‘logging’ argument:
  7. 7. Beginner’s mistake This function based sleep decorator lacks nesting. Calls broken_sleep_decorator(say_hello) upon program initialization; so sleep(1) is always called. Function is not replaced
  8. 8. Working sleep decorator Defines ‘wrapper’ and returns it as replacement function Calls original function Executes say_hello = sleep_decorator(say_hello) Calls wrapper(“Byte”) Call is equal to: sleep_decorator(say_hello)(“Byte”)
  9. 9. Tunable sleep decorator Accepts decorator arguments Accepts function argument Accepts arguments for function Calls original function Calls tunable_sleep_decorator (seconds=1) (say_hello) Calls wrapper(“Byte”) Call is equal to: tunable_sleep_decorator(seconds=1)(say_hello)(“Byte”)
  10. 10. Function vs Class decorators ● They can be written to have near identical behaviour; ● However, classes allow storage of state. ● It depends on the situation, really.
  11. 11. Function vs. class decorators ● Basic function vs. class example ● Note the difference in return values
  12. 12. Function vs. Class decorators A counter that stores state
  13. 13. Debug decorator Call function with args, surrounded by print statements
  14. 14. Basic benchmark decorator
  15. 15. Retry decorator
  16. 16. Further reading ● ● ● ● ● ● ● http://www.python.org/dev/peps/pep-0318/ https://wiki.python.org/moin/PythonDecorators https://wiki.python.org/moin/PythonDecoratorLibrary http://www.artima.com/weblogs/viewpost.jsp?thread=240808 http://pythonconquerstheuniverse.wordpress.com/2012/04/29/python-decorators/ http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-inpython/1594484#1594484 http://www.ibm.com/developerworks/library/l-cpdecor/ code examples from this presentation: @rikva/decorators

×