Slide 1

Slide 1 text

Decorators, unwrapped Katie Silverio @astrosilverio

Slide 2

Slide 2 text

@astrosilverio I use decorators but they are I can show you how they ⚙!

Slide 3

Slide 3 text

@astrosilverio Decorators, unwrapped ✴ Formal definition of a decorator ✴ Anatomy of a decorator ✴ Why it works ✴ Common tricks and gotchas

Slide 4

Slide 4 text

@astrosilverio What is a decorator? ✨examples✨

Slide 5

Slide 5 text

@astrosilverio What is a decorator? @timer def my_function(): # code goes here > my_function() my_function ran in 0.005s > my_function() my_function ran in 0.008s

Slide 6

Slide 6 text

@astrosilverio What is a decorator? class MyClass(object): @classmethod def no_need_for_an_instance(cls): # some code goes here > MyClass.no_need_for_an_instance() some output

Slide 7

Slide 7 text

@astrosilverio A decorator changes the behavior of a function without the user having to change the function code itself Also a decorator can be used to decorate many functions!

Slide 8

Slide 8 text

@astrosilverio Time this! def my_function(arg): # some code goes here return my_return_value > result = my_function(some_value) 0.11s > result = my_function(some_other_value) 1.01s

Slide 9

Slide 9 text

@astrosilverio Timing, constructed import time def my_function(arg): start_time = time.time() # original body of original function end_time = time.time() print end_time - start_time return original_return_value

Slide 10

Slide 10 text

@astrosilverio Timing, decorated import my_timer # how does it work? a mystery @my_timer def my_function(arg): # original body of function return original_return_value @my_timer def foo(bar): # foo the bar return baz

Slide 11

Slide 11 text

@astrosilverio What is a function? ✨a first-class object✨

Slide 12

Slide 12 text

@astrosilverio What is a first-class object? ✴ FCOs can be assigned to variables ✴ FCOs can be passed as arguments to other functions ✴ FCOs can be returned from other functions ✴ FCOs can be created within functions

Slide 13

Slide 13 text

@astrosilverio Refactoring goals ✴ Decouple timing code from function code ✴ Make timing code reusable

Slide 14

Slide 14 text

@astrosilverio Back to timing import time def timing_wrapper(func, func_arg): start_time = time.time() value_to_return = func(func_arg) end_time = time.time() print end_time - start_time return value_to_return > timing_wrapper(my_function, some_arg)

Slide 15

Slide 15 text

@astrosilverio Refactoring goals ✴ Decouple timing code from function code ✴ Make timing code reusable ✴ Wrap functions with timing code at definition time

Slide 16

Slide 16 text

@astrosilverio Back to timing import time def my_timer(func): # creates a new function # that executes `func` AND # performs timing code return new_function timed_function = my_timer(my_function)

Slide 17

Slide 17 text

@astrosilverio Final refactor! import time def my_timer(func): def new_wrapped_function(func_arg): start_time = time.time() value_to_return = func(func_arg) end_time = time.time() print end_time - start_time return value_to_return return new_wrapped_function

Slide 18

Slide 18 text

@astrosilverio Final refactor! import my_timer def foo(bar): # foo the bar return baz foo = my_timer(foo)

Slide 19

Slide 19 text

@astrosilverio Back to decorators From PEP 318:

Slide 20

Slide 20 text

@astrosilverio Back to decorators import my_timer def foo(bar): # foo the bar foo = my_timer(foo) import my_timer @my_timer def foo(bar): # foo the bar is equivalent to

Slide 21

Slide 21 text

@astrosilverio A decorator replaces a function with the decorator's own return value, when called with the function it decorates

Slide 22

Slide 22 text

@astrosilverio General decorator form def my_decorator(func): def new_function(*args, **kwargs): # body probably contains some # new code and probably a call # to func and probably returns # the return value of func # probably return new_function

Slide 23

Slide 23 text

@astrosilverio "Probably" def no_op(func): return func def sleight_of_hand(func): def answer_is_42(*args, **kwargs): return 42 return answer_is_42 def black_hole(func): return None

Slide 24

Slide 24 text

@astrosilverio Open Questions ✴ How does the inner function have access to the wrapped function? ✴ Why do many people use @wraps? ✴ What order are stacked decorators applied in? ✴ How does the @ syntactic sugar work?

Slide 25

Slide 25 text

@astrosilverio Scope def my_function(some_arg): print some_arg # works

Slide 26

Slide 26 text

@astrosilverio Scope def my_function(some_arg): print some_arg # works print locals() > my_function(1) 1 {'some_arg': 1}

Slide 27

Slide 27 text

@astrosilverio Scope def my_function(some_arg): print some_arg # works def inner_function(): print some_arg # works print locals() return inner_function > my_function(42)() 42 42 {'some_arg': 42}

Slide 28

Slide 28 text

@astrosilverio Open Questions ✴ How does the inner function have access to the wrapped function? ✴ Why do many people use @wraps? ✴ What order are stacked decorators applied in? ✴ How does the @ syntactic sugar work?

Slide 29

Slide 29 text

@astrosilverio Timing decorator import time def my_timer(func): def new_wrapped_function(func_arg): start_time = time.time() value_to_return = func(func_arg) end_time = time.time() print end_time - start_time return value_to_return return new_wrapped_function

Slide 30

Slide 30 text

@astrosilverio Misdirection import my_timer @my_timer def foo(bar): # foo the bar! return baz > foo.__name__ new_wrapped_function # ???

Slide 31

Slide 31 text

@astrosilverio Enter @wraps! ✴ Replaces the wrapper's __name__ with the name of the wrapped function ✴ Replaces the wrapper's __doc__ with the docstring of the wrapped function ✴ Does this through decorating the wrapper

Slide 32

Slide 32 text

@astrosilverio Fix it with decorators import time from functools import wraps def my_timer(func): @wraps def new_wrapped_function(func_arg): start_time = time.time() value_to_return = func(func_arg) end_time = time.time() print end_time - start_time return value_to_return return new_wrapped_function

Slide 33

Slide 33 text

@astrosilverio Open Questions ✴ How does the inner function have access to the wrapped function? ✴ Why do many people use @wraps? ✴ What order are stacked decorators applied in? ✴ How does the @ syntactic sugar work?

Slide 34

Slide 34 text

@astrosilverio Decorators can stack! import my_timer import another_decorator @another_decorator @my_timer def foo(bar): # do something! return baz

Slide 35

Slide 35 text

@astrosilverio Remember @my_timer def foo(bar): # do something! foo = my_timer(foo) is equivalent to

Slide 36

Slide 36 text

@astrosilverio What is happening? foo = another_decorator(my_timer(foo)) foo = my_timer(another_decorator(foo)) or

Slide 37

Slide 37 text

@astrosilverio Always wrap down @another_decorator @my_timer def foo(bar): # do something! foo = another_decorator(my_timer(foo)) is equivalent to

Slide 38

Slide 38 text

@astrosilverio More Questions ✴ How does the @ syntactic sugar work? ✴ How do decorators that take arguments work? ✴ How do class decorators work? ✴ How do you write a decorator that is not a function? ✴ How does @wraps work?

Slide 39

Slide 39 text

@astrosilverio How do decorators work?

Slide 40

Slide 40 text

@astrosilverio Decorators, in brief ✴ @decorator is really syntactic sugar for object = decorator(object) ✴ no other rules!

Slide 41

Slide 41 text

@astrosilverio Further Reading ✴ http://simeonfranklin.com/blog/2012/jul/1/ python-decorators-in-12-steps/ ✴ https://www.thecodeship.com/patterns/guide- to-python-function-decorators/ ✴ https://www.python.org/dev/peps/pep-0318/