Skip to content

Commit 3a59ddd

Browse files
author
KOTP
committed
White space "corrections"
1 parent 19c4cc6 commit 3a59ddd

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

concepts/decorators/about.md

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Decorators are functions that take another function as an argument for the purpose of extending or replacing the behavior of the passed-in function.
44
If function `A` is a decorator, and function `B` is its argument, then function `A` modifies, extends or replaces function `B`'s **behavior** _without modifying_ function `B`'s code.
55
We say that the decorator function `A` _wraps_ function `B`.
6-
While we talk about "modifying" behavior, the wrapped function is _not actually changed_.
6+
While we talk about "modifying" behavior, the wrapped function is _not actually changed_.
77
Behavior is either added _around_ the wrapped function (_and what it returns_), or the wrapped function's behavior is _substituted_ for some other behavior.
88

99
## A Decorator is a Higher-Order Function
@@ -31,7 +31,7 @@ def decorated_function2():
3131
pass
3232
```
3333

34-
If a decorator has defined default arguments, you must use parenthesis in the `@decorator()` call for the decorator to work:
34+
If a decorator has defined default arguments, you must use parenthesis in the `@decorator()` call for the decorator to work:
3535

3636
```python
3737
@decorator_with_default_arg()
@@ -78,14 +78,13 @@ A simple decorator - one that simply returns its wrapped function - can be writt
7878
```python
7979
>>> def do_nothing(func):
8080
... return func
81-
...
81+
...
8282
... @do_nothing
8383
... def function4():
8484
... return 4
85-
...
85+
...
8686
>>> print(function4())
8787
4
88-
8988
```
9089

9190
A decorator may only add side effects, such as additional information used for logging:
@@ -94,15 +93,14 @@ A decorator may only add side effects, such as additional information used for l
9493
>>> def my_logger(func):
9594
... print(f"Entering {func.__name__}")
9695
... return func
97-
...
96+
...
9897
... @my_logger
9998
... def my_func():
10099
... print("Hello")
101-
...
100+
...
102101
>>> my_func()
103102
Entering my_func
104103
Hello
105-
106104
```
107105

108106
A decorator does not return itself.
@@ -130,19 +128,18 @@ Following is an example of a decorator being used for validation:
130128
... else:
131129
... return func(world)
132130
... return my_wrapper
133-
...
131+
...
134132
... @my_validator
135133
... def my_func(planet):
136134
... print(f"Hello, {planet}!")
137-
...
135+
...
138136
>>> my_func("World")
139137
Entering my_func with World argument
140138
Hello, World!
141139
...
142140
>>> my_func("Pluto")
143141
Entering my_func with Pluto argument
144142
Pluto is not a planet!
145-
146143
```
147144

148145
On the first line, we have the definition for the decorator with its `func` argument.
@@ -169,16 +166,15 @@ Following is an example of a decorator for a function that takes an arbitrary nu
169166
... def wrapper(*args, **kwargs):
170167
... return func(*args, **kwargs) * 2
171168
... return wrapper
172-
...
169+
...
173170
... @double
174171
... def add(*args):
175172
... return sum(args)
176-
...
173+
...
177174
>>> print(add(2, 3, 4))
178175
18
179176
>>> print(add(2, 3, 4, 5, 6))
180177
40
181-
182178
```
183179

184180
This works for doubling the return value from the function argument.
@@ -192,22 +188,21 @@ Following is an example of a decorator that can be configured to multiply the de
192188
>>> def multi(factor=1):
193189
... if (factor == 0):
194190
... raise ValueError("factor must not be 0")
195-
...
191+
...
196192
... def outer_wrapper(func):
197193
... def inner_wrapper(*args, **kwargs):
198194
... return func(*args, **kwargs) * factor
199195
... return inner_wrapper
200196
... return outer_wrapper
201-
...
197+
...
202198
... @multi(factor=3)
203199
... def add(*args):
204200
... return sum(args)
205-
...
201+
...
206202
>>> print(add(2, 3, 4))
207203
27
208204
>>> print(add(2, 3, 4, 5, 6))
209205
60
210-
211206
```
212207

213208
The first lines validate that `factor` is not `0`.
@@ -230,18 +225,17 @@ Following is an example of a parameterized decorator that controls whether it va
230225
... return func(world)
231226
... return my_wrapper
232227
... return my_validator
233-
...
228+
...
234229
... @check_for_pluto(check=False)
235230
... def my_func(planet):
236231
... print(f"Hello, {planet}!")
237-
...
232+
...
238233
>>> my_func("World")
239234
Entering my_func with World argument
240235
Hello, World!
241236
>>> my_func("Pluto")
242237
Entering my_func with Pluto argument
243238
Hello, Pluto!
244-
245239
```
246240

247241
This allows for easy toggling between checking for `Pluto` or not, and is done without having to modify `my_func`.

0 commit comments

Comments
 (0)