|
| 1 | +--- |
| 2 | +title:Python Decorators - Python Cheatsheet |
| 3 | +description:A Python Decorator is a syntax that provide a concise and reusable way for extending a function or a class. |
| 4 | +--- |
| 5 | + |
| 6 | +<base-title:title="frontmatter.title":description="frontmatter.description"> |
| 7 | +Python Decorators |
| 8 | +</base-title> |
| 9 | + |
| 10 | +A Python Decorator is a syntax that provides a concise and reusable way for extending a function or a class. |
| 11 | + |
| 12 | +##Bare bone decorator |
| 13 | + |
| 14 | +A decorator in its simplest form is a function that takes another function as an argument and returns a wrapper function. The following example shows the creation of a decorator and its usage. |
| 15 | + |
| 16 | +```python |
| 17 | +defyour_decorator(func): |
| 18 | +defwrapper(): |
| 19 | +# Do stuff before func... |
| 20 | +print("Before func!") |
| 21 | + func() |
| 22 | +# Do stuff after func... |
| 23 | +print("After func!") |
| 24 | +return wrapper |
| 25 | + |
| 26 | +@your_decorator |
| 27 | +deffoo(): |
| 28 | +print("Hello World!") |
| 29 | + |
| 30 | +foo() |
| 31 | + |
| 32 | +# Before func! |
| 33 | +# Hello World! |
| 34 | +# After func! |
| 35 | +``` |
| 36 | + |
| 37 | +##Decorator for a function with parameters |
| 38 | + |
| 39 | +```python |
| 40 | +defyour_decorator(func): |
| 41 | +defwrapper(*args,**kwargs): |
| 42 | +# Do stuff before func... |
| 43 | +print("Before func!") |
| 44 | + func(*args,**kwargs) |
| 45 | +# Do stuff after func... |
| 46 | +print("After func!") |
| 47 | +return wrapper |
| 48 | + |
| 49 | +@your_decorator |
| 50 | +deffoo(bar): |
| 51 | +print("My name is"+ bar) |
| 52 | + |
| 53 | +foo("Jack") |
| 54 | + |
| 55 | +# Before func! |
| 56 | +# My name is Jack |
| 57 | +# After func! |
| 58 | +``` |
| 59 | + |
| 60 | +##Template for a basic decorator |
| 61 | + |
| 62 | +This template is useful for most decorator use-cases. It is valid for functions with or without parameters, and with or without a return value. |
| 63 | + |
| 64 | +```python |
| 65 | +import functools |
| 66 | + |
| 67 | +defyour_decorator(func): |
| 68 | +@functools.wraps(func)# For preserving the metadata of func. |
| 69 | +defwrapper(*args,**kwargs): |
| 70 | +# Do stuff before func... |
| 71 | + result= func(*args,**kwargs) |
| 72 | +# Do stuff after func.. |
| 73 | +return result |
| 74 | +return wrapper |
| 75 | +``` |
| 76 | + |
| 77 | +##Decorator with parameters |
| 78 | + |
| 79 | +You can also define parameters for the decorator to use. |
| 80 | + |
| 81 | +```python |
| 82 | +import functools |
| 83 | + |
| 84 | +defyour_decorator(arg): |
| 85 | +defdecorator(func): |
| 86 | +@functools.wraps(func)# For preserving the metadata of func. |
| 87 | +defwrapper(*args,**kwargs): |
| 88 | +# Do stuff before func possibly using arg... |
| 89 | + result= func(*args,**kwargs) |
| 90 | +# Do stuff after func possibly using arg... |
| 91 | +return result |
| 92 | +return wrapper |
| 93 | +return decorator |
| 94 | +``` |
| 95 | + |
| 96 | +To use this decorator: |
| 97 | + |
| 98 | +```python |
| 99 | +@your_decorator(arg='x') |
| 100 | +deffoo(bar): |
| 101 | +return bar |
| 102 | +``` |
| 103 | + |
| 104 | +##Class based decorators |
| 105 | + |
| 106 | +A decorator can also be defined as a class instead of a method. This is useful for maintaining and updating a state, such as in the following example, where we count the number of calls made to a method: |
| 107 | + |
| 108 | +```python |
| 109 | +classCountCallNumber: |
| 110 | + |
| 111 | +def__init__(self,func): |
| 112 | +self.func= func |
| 113 | +self.call_number=0 |
| 114 | + |
| 115 | +def__call__(self,*args,**kwargs): |
| 116 | +self.call_number+=1 |
| 117 | +print("This is execution number"+str(self.call_number)) |
| 118 | +returnself.func(*args,**kwargs) |
| 119 | + |
| 120 | +@CountCallNumber |
| 121 | +defsay_hi(name): |
| 122 | +print("Hi! My name is"+ name) |
| 123 | + |
| 124 | +say_hi("Jack") |
| 125 | + |
| 126 | +say_hi("James") |
| 127 | + |
| 128 | +# This is execution number 1 |
| 129 | +# Hi! My name is Jack |
| 130 | +# This is execution number 2 |
| 131 | +# Hi! My name is James |
| 132 | +``` |
| 133 | +<base-disclaimer> |
| 134 | + <base-disclaimer-title> |
| 135 | +Count Example |
| 136 | + </base-disclaimer-title> |
| 137 | + <base-disclaimer-content> |
| 138 | + This count example is inspired by Patrick Loeber's <ahref="https://youtu.be/HGOBQPFzWKo?si=IUvFzeQbzTmeEgKV"target="_blank">YouTube tutorial</a>. |
| 139 | + </base-disclaimer-content> |
| 140 | +</base-disclaimer> |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |