Your SlideShare is downloading. ×
Introduction to Python decorators
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Introduction to Python decorators

842
views

Published on

Presentation held at the Amsterdam Python Meetup Group

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
842
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Python Decorators Introduction to Python decorators Rik van Achterberg | 2013-12-04
  • 2. You might have seen them
  • 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. Decorators replace functions Function being replaced by class Function being replaced by (inner) function
  • 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. How decorators are being called Identical behaviour with and without decorators Adding ‘logging’ argument:
  • 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. 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. 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. 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. Function vs. class decorators ● Basic function vs. class example ● Note the difference in return values
  • 12. Function vs. Class decorators A counter that stores state
  • 13. Debug decorator Call function with args, surrounded by print statements
  • 14. Basic benchmark decorator
  • 15. Retry decorator
  • 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