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/blog/python-decorators-for-beginners.md
+32-16Lines changed: 32 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ meta:
20
20
21
21
<blog-title-header:frontmatter="frontmatter"title="Python Decorators: Simple Patterns to Level Up Your Code" />
22
22
23
-
You know that feeling when you see `@something` above a function and wonder what black magic is happening? I've been there too. Decorators might look intimidating, but they're actually one of Python's most elegant features once you understand the basics.
23
+
You know that feeling when you see `@something` above a function and wonder what black magic is happening? I've been there too. Decorators might look intimidating, but they're actually one of Python's most elegant features once you understand the basics — see <router-linkto="/cheatsheet/decorators">Decorators (cheatsheet)</router-link> for a compact reference.
24
24
25
25
Think of decorators as gift wrapping for your functions. The function inside stays the same, but the decorator adds a nice bow on top – extra functionality without changing the original code.
26
26
@@ -74,13 +74,15 @@ def slow_function():
74
74
result = slow_function()
75
75
# slow_function took 1.0041 seconds
76
76
print(result) # Done!
77
+
78
+
See the <router-link to="/cheatsheet/decorators">Decorators (cheatsheet)</router-link>for additional decorator templates and common patterns.
77
79
```
78
80
79
-
Notice how we use `*args` and `**kwargs`? This makes our decorator work with any function, regardless of how many arguments it takes.
81
+
Notice how we use `*args`and`**kwargs` (see <router-link to="/cheatsheet/args-and-kwargs">Args & kwargs</router-link>)? This makes our decorator work withany function, regardless of how many arguments it takes.
80
82
81
83
## Debug Your Code: Logger Decorator
82
84
83
-
When you're trying to figure out what's going wrong, this decorator is incredibly handy:
85
+
When you're trying to figure out what's going wrong, this decorator is incredibly handy — also check the <router-link to="/cheatsheet/debugging">Debugging (cheatsheet)</router-link>for complementary tips and techniques:
84
86
85
87
```python
86
88
def debug(func):
@@ -90,7 +92,7 @@ def debug(func):
90
92
kwargs_str = ', '.join(f"{k}={v!r}"for k, v in kwargs.items())
f"{param_name} must be {expected_type.__name__}, "
263
265
f"got {type(value).__name__}"
264
266
)
265
-
267
+
266
268
return func(*args, **kwargs)
267
269
return wrapper
268
270
return decorator
@@ -297,7 +299,7 @@ except TypeError as e:
297
299
298
300
## Tips for Using Decorators
299
301
300
-
**Always use `@functools.wraps`** – This preserves the original function's name and documentation, making debugging easier.
302
+
**Always use `@functools.wraps`** – This preserves the original function's name and documentation, making debugging easier (see <router-linkto="/cheatsheet/decorators">Decorators cheatsheet</router-link> for examples).
301
303
302
304
**Keep them simple** – If your decorator is getting complex, consider if it should be a class or separate function instead.
303
305
@@ -321,4 +323,18 @@ Decorators let you add functionality to functions without changing their code. T
321
323
322
324
Start with the simple patterns shown here. Once you're comfortable, you can create more sophisticated decorators for your specific needs. The key is understanding that decorators are just functions that wrap other functions – everything else is just clever applications of that basic concept.
323
325
324
-
Want to practice? Try adding the `@timer` decorator to some of your existing functions and see which ones are slower than you expected. You might be surprised at what you discover!
326
+
Want to practice? Try adding the `@timer` decorator to some of your existing functions and see which ones are slower than you expected. You might be surprised at what you discover!
327
+
328
+
## Relevant Links
329
+
330
+
Add relevant internal documentation links below for further reading:
Copy file name to clipboardExpand all lines: docs/cheatsheet/decorators.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Python Decorators
8
8
</base-title>
9
9
10
10
A Python Decorator provides a concise and reusable way for extending
11
-
a function or a class.
11
+
a function or a class. Read the companion article <router-linkto="/blog/python-decorators-for-beginners">Python Decorators: Simple Patterns to Level Up Your Code</router-link> for practical examples and patterns.
12
12
13
13
## Bare bone decorator
14
14
@@ -182,9 +182,10 @@ say_hi("James")
182
182
183
183
## Relevant links
184
184
185
+
- <router-linkto="/blog/python-decorators-for-beginners">Python Decorators: Simple Patterns to Level Up Your Code</router-link>
186
+
- <router-linkto="/blog/python-easy-args-kwargs">Python *args and **kwargs Made Easy</router-link>
0 commit comments