Skip to content

Commit 3e3a1c8

Browse files
authored
Merge pull request wilfredinni#470 from fPkX6F1nGTX/fPkX6F1nGTX-decorators-for-class-methods
Example of decorator used within a class for its own method
2 parents 3126656 + 369ab47 commit 3e3a1c8

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

docs/cheatsheet/decorators.md

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ description: A Python Decorator is a syntax that provide a concise and reusable
77
Python Decorators
88
</base-title>
99

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.
1112

1213
## Bare bone decorator
1314

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.
1518

1619
```python
1720
def your_decorator(func):
@@ -58,7 +61,8 @@ foo("Jack")
5861

5962
## Template for a basic decorator
6063

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.
6266

6367
```python
6468
import functools
@@ -102,7 +106,46 @@ def foo(bar):
102106

103107
## Class based decorators
104108

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
113+
and print exceptions in a certain way.
114+
115+
```python
116+
class DecorateMyMethod:
117+
118+
def decorator_for_class_method_with_no_args(method):
119+
def wrapper_for_class_method(self)
120+
try:
121+
return method(self)
122+
except Exception as e:
123+
print("\nWARNING: Please make note of the following:\n")
124+
print(e)
125+
return wrapper_for_class_method
126+
127+
def __init__(self,succeed:bool):
128+
self.succeed = succeed
129+
130+
@decorator_for_class_method_with_no_args
131+
def class_action(self):
132+
if self.succeed:
133+
print("You succeeded by choice.")
134+
else:
135+
raise Exception("Epic fail of your own creation.")
136+
137+
test_succeed = DecorateMyMethods(True)
138+
test_succeed.class_action()
139+
# You succeeded by choice.
140+
141+
test_fail = DecorateMyMethod(False)
142+
test_fail.class_action()
143+
# Exception: Epic fail of your own creation.
144+
```
145+
146+
A decorator can also be defined as a class instead of a method. This is useful
147+
for maintaining and updating a state, such as in the following example, where we
148+
count the number of calls made to a method:
106149

107150
```python
108151
class CountCallNumber:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy