You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/cheatsheet/decorators.md
+47-4
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,14 @@ description: A Python Decorator is a syntax that provide a concise and reusable
7
7
Python Decorators
8
8
</base-title>
9
9
10
-
A Python Decorator provides a concise and reusable way for extending a function or a class.
10
+
A Python Decorator provides a concise and reusable way for extending
11
+
a function or a class.
11
12
12
13
## Bare bone decorator
13
14
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.
15
+
A decorator in its simplest form is a function that takes another
16
+
function as an argument and returns a wrapper. The following example
17
+
shows the creation of a decorator and its usage.
15
18
16
19
```python
17
20
defyour_decorator(func):
@@ -58,7 +61,8 @@ foo("Jack")
58
61
59
62
## Template for a basic decorator
60
63
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.
64
+
This template is useful for most decorator use-cases. It is valid for functions
65
+
with or without parameters, and with or without a return value.
62
66
63
67
```python
64
68
import functools
@@ -102,7 +106,46 @@ def foo(bar):
102
106
103
107
## Class based decorators
104
108
105
-
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:
109
+
To decorate a class methos, you must define the decorator within the class. When
110
+
only the implicit argument `self` is passed to the method, without any other
111
+
additional arguments, you must make a separate decorator for only those methods
112
+
without any additional arguments. An example of this is when you want to catch
0 commit comments