introduction to python decorators

16
Python Decorators Introduction to Python decorators Rik van Achterberg | 2013-12-04

Upload: rikbyte

Post on 20-May-2015

918 views

Category:

Technology


9 download

DESCRIPTION

Presentation held at the Amsterdam Python Meetup Group

TRANSCRIPT

Page 1: Introduction to Python decorators

Python DecoratorsIntroduction to Python decorators

Rik van Achterberg | 2013-12-04

Page 2: Introduction to Python decorators

You might have seen them

Page 3: Introduction to Python decorators

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.

Page 4: Introduction to Python decorators

Decorators replace functions

Function being replaced by class

Function being replaced by (inner) function

Page 5: Introduction to Python decorators

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.

Page 6: Introduction to Python decorators

Identical behaviour with and without

decorators

Adding ‘logging’ argument:

How decorators are being called

Page 7: Introduction to Python decorators

Beginner’s mistakeThis 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

Page 8: Introduction to Python decorators

Working sleep decorator

Executessay_hello = sleep_decorator(say_hello)

Calls wrapper(“Byte”)

Call is equal to:sleep_decorator(say_hello)(“Byte”)

Defines ‘wrapper’ and returns it as replacement function

Calls original function

Page 9: Introduction to Python decorators

Tunable sleep decorator

Calls tunable_sleep_decorator(seconds=1)(say_hello)

Calls wrapper(“Byte”)

Call is equal to:tunable_sleep_decorator(seconds=1)(say_hello)(“Byte”)

Accepts decorator arguments

Accepts function argument

Accepts arguments for function

Calls original function

Page 10: Introduction to Python decorators

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.

Page 11: Introduction to Python decorators

Function vs. class decorators

● Basic function vs. class example

● Note the difference in return values

Page 12: Introduction to Python decorators

Function vs. Class decoratorsA counter that stores state

Page 13: Introduction to Python decorators

Debug decoratorCall function with args, surrounded by

print statements

Page 14: Introduction to Python decorators

Basic benchmark decorator

Page 15: Introduction to Python decorators

Retry decorator

Page 16: Introduction to Python decorators

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-in-

python/1594484#1594484

● http://www.ibm.com/developerworks/library/l-cpdecor/

code examples from this presentation:

@rikva/decorators