1 / 11

An intro to Python decorators

Graeme Cross MPUG, March 2011. An intro to Python decorators. Housekeeping. The audience: new – intermediate Python programmers who have not used decorators This talk (and example code) is online at: hg clone https://gjcross@bitbucket.org/gjcross/talks. Interior decorating, Python style.

magar
Download Presentation

An intro to Python decorators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graeme Cross MPUG, March 2011 An intro to Python decorators

  2. Housekeeping • The audience: new – intermediate Python programmers who have not used decorators • This talk (and example code) is online at: hg clone https://gjcross@bitbucket.org/gjcross/talks

  3. Interior decorating, Python style • Decorators modify or extend functions, methods or classes • That’s it! • They are not special, just convenient • Simple syntax: @decorator

  4. Decorators and decorators • Python 2.4: function/method decorators • Python 2.6: class decorators • Python’s decorator != GOF Decorator pattern • Note: this talk will just cover function/method decorators (not class decorators)

  5. Why bother? • Decorators help with: • Readability • Less lines of code • Explicit behaviour • Code maintenance • Minimising code duplication • Robust design • Separation of concerns • Widely used in Python libraries

  6. Out of the box... • Python has a range of built-in decorators • For example: @property @staticmethod @classmethod

  7. Defining a decorator def decorator(f) # do something with f return f @decorator def myfunc(x): # my function’s code here...

  8. An example... • Two examples of how to use class properties: • property.py • decorator_prop.py

  9. Typical uses • Mimic Eiffel’s preconditions/postconditions • Check values and/or types • Cache results • Record function results, entry and exit times/states/values • Simplify synchronisation, locking • Enable debugging, logging, tracing and profiling

  10. Roll your own! • An example of defining our own decorator • If we have a computationally intensive algorithm: • Seen the input before? • No: Calculate and cache the result (takes a while) • Yes: Return the cached result (fast) • Example: • cache.py

  11. Want to know more? • http://wiki.python.org/moin/PythonDecorators • http://wiki.python.org/moin/PythonDecoratorLibrary • http://www.python.org/dev/peps/pep-0318/ • “Learning Python”, 4th edition, chapter 38 • http://www.ibm.com/developerworks/linux/library/l-cpdecor.html • http://code.activestate.com/recipes/langs/python/tags/decorator/

More Related