|
1 | 1 | --- |
2 | 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. |
| 3 | +description:A Python Decorator is a syntax that provide a concise |
| 4 | +and reusable way for extending a function or a class. |
4 | 5 | --- |
5 | 6 |
|
6 | 7 | <base-title:title="frontmatter.title":description="frontmatter.description"> |
7 | 8 | Python Decorators |
8 | 9 | </base-title> |
9 | 10 |
|
10 | | -A Python Decorator provides a concise and reusable way for extending a function or a class. |
| 11 | +A Python Decorator provides a concise and reusable way for extending |
| 12 | +a function or a class. |
11 | 13 |
|
12 | 14 | ##Bare bone decorator |
13 | 15 |
|
14 | | -A decorator in its simplest form is a function that takes another function as an argument and returns a wrapper. The following example shows the creation of a decorator and its usage. |
| 16 | +A decorator in its simplest form is a function that takes another |
| 17 | +function as an argument and returns a wrapper. The following example |
| 18 | +shows the creation of a decorator and its usage. |
15 | 19 |
|
16 | 20 | ```python |
17 | 21 | defyour_decorator(func): |
@@ -58,7 +62,8 @@ foo("Jack") |
58 | 62 |
|
59 | 63 | ##Template for a basic decorator |
60 | 64 |
|
61 | | -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. |
| 65 | +This template is useful for most decorator use-cases. It is valid for functions |
| 66 | +with or without parameters, and with or without a return value. |
62 | 67 |
|
63 | 68 | ```python |
64 | 69 | import functools |
@@ -102,7 +107,11 @@ def foo(bar): |
102 | 107 |
|
103 | 108 | ##Class based decorators |
104 | 109 |
|
105 | | -To decorate a class methos, you must define the decorator within the class. When only the implicit argument`self` is passed to the method, without any other additional arguments, you must make a separate decorator for only those methods without any additional arguments. An example of this is when you want to catch and print exceptions in a certain way. |
| 110 | +To decorate a class methos, you must define the decorator within the class. When |
| 111 | +only the implicit argument`self` is passed to the method, without any other |
| 112 | +additional arguments, you must make a separate decorator for only those methods |
| 113 | +without any additional arguments. An example of this is when you want to catch |
| 114 | +and print exceptions in a certain way. |
106 | 115 |
|
107 | 116 | ```python |
108 | 117 | classDecorateMyMethod: |
@@ -135,7 +144,9 @@ test_fail.class_action() |
135 | 144 | # Exception: Epic fail of your own creation. |
136 | 145 | ``` |
137 | 146 |
|
138 | | -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: |
| 147 | +A decorator can also be defined as a class instead of a method. This is useful |
| 148 | +for maintaining and updating a state, such as in the following example, where we |
| 149 | +count the number of calls made to a method: |
139 | 150 |
|
140 | 151 | ```python |
141 | 152 | classCountCallNumber: |
|