Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81fe9ad

Browse files
Add relevant links to documentation for decorators and enhance spell check entries
1 parent 2cf8b5d commit 81fe9ad

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

‎.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"levelname",
110110
"listdir",
111111
"ljust",
112+
"Loeber's",
112113
"lstrip",
113114
"lukaszwojcik",
114115
"madeelchaudhary",

‎docs/blog/python-decorators-for-beginners.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ meta:
2020

2121
<blog-title-header :frontmatter="frontmatter" title="Python Decorators: Simple Patterns to Level Up Your Code" />
2222

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

2525
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.
2626

@@ -74,13 +74,15 @@ def slow_function():
7474
result = slow_function()
7575
# slow_function took 1.0041 seconds
7676
print(result) # Done!
77+
78+
See the <router-link to="/cheatsheet/decorators">Decorators (cheatsheet)</router-link> for additional decorator templates and common patterns.
7779
```
7880

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 with any function, regardless of how many arguments it takes.
8082

8183
## Debug Your Code: Logger Decorator
8284

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:
8486

8587
```python
8688
def debug(func):
@@ -90,7 +92,7 @@ def debug(func):
9092
kwargs_str = ', '.join(f"{k}={v!r}" for k, v in kwargs.items())
9193
all_args = ', '.join(filter(None, [args_str, kwargs_str]))
9294
print(f"Calling {func.__name__}({all_args})")
93-
95+
9496
result = func(*args, **kwargs)
9597
print(f"{func.__name__} returned {result!r}")
9698
return result
@@ -116,10 +118,10 @@ def requires_auth(func):
116118
def wrapper(*args, **kwargs):
117119
# In a real app, you'd check actual authentication
118120
user_logged_in = True # This would come from your auth system
119-
121+
120122
if not user_logged_in:
121123
return "Access denied! Please log in."
122-
124+
123125
return func(*args, **kwargs)
124126
return wrapper
125127

@@ -139,18 +141,18 @@ If you have a function that does expensive calculations with the same inputs, ca
139141
```python
140142
def cache(func):
141143
cached_results = {}
142-
144+
143145
@functools.wraps(func)
144146
def wrapper(*args):
145147
if args in cached_results:
146148
print(f"Cache hit for {func.__name__}{args}")
147149
return cached_results[args]
148-
150+
149151
print(f"Computing {func.__name__}{args}")
150152
result = func(*args)
151153
cached_results[args] = result
152154
return result
153-
155+
154156
return wrapper
155157

156158
@cache
@@ -215,16 +217,16 @@ import time
215217
def rate_limit(seconds):
216218
def decorator(func):
217219
last_called = [^0] # Use list to store mutable value
218-
220+
219221
@functools.wraps(func)
220222
def wrapper(*args, **kwargs):
221223
elapsed = time.time() - last_called
222224
if elapsed < seconds:
223225
time.sleep(seconds - elapsed)
224-
226+
225227
last_called = time.time()
226228
return func(*args, **kwargs)
227-
229+
228230
return wrapper
229231
return decorator
230232

@@ -253,7 +255,7 @@ def validate_types(**expected_types):
253255
sig = inspect.signature(func)
254256
bound_args = sig.bind(*args, **kwargs)
255257
bound_args.apply_defaults()
256-
258+
257259
for param_name, expected_type in expected_types.items():
258260
if param_name in bound_args.arguments:
259261
value = bound_args.arguments[param_name]
@@ -262,7 +264,7 @@ def validate_types(**expected_types):
262264
f"{param_name} must be {expected_type.__name__}, "
263265
f"got {type(value).__name__}"
264266
)
265-
267+
266268
return func(*args, **kwargs)
267269
return wrapper
268270
return decorator
@@ -297,7 +299,7 @@ except TypeError as e:
297299

298300
## Tips for Using Decorators
299301

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).
301303

302304
**Keep them simple** – If your decorator is getting complex, consider if it should be a class or separate function instead.
303305

@@ -321,4 +323,18 @@ Decorators let you add functionality to functions without changing their code. T
321323

322324
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.
323325

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:
331+
332+
- <router-link to="/cheatsheet/decorators">Decorators (cheatsheet)</router-link>
333+
- <router-link to="/cheatsheet/functions">Functions (cheatsheet)</router-link>
334+
- <router-link to="/cheatsheet/args-and-kwargs">Args & kwargs (cheatsheet)</router-link>
335+
- <router-link to="/cheatsheet/debugging">Debugging (cheatsheet)</router-link>
336+
- <router-link to="/cheatsheet/oop-basics">OOP basics (cheatsheet)</router-link>
337+
- <router-link to="/builtin/callable">callable() (builtin)</router-link>
338+
- <router-link to="/builtin/staticmethod">staticmethod (builtin)</router-link>
339+
- <router-link to="/blog/python-easy-args-kwargs">Python: Easy args & kwargs (blog)</router-link>
340+
- <router-link to="/blog/python-comprehensions-step-by-step">Python comprehensions (blog)</router-link>

‎docs/cheatsheet/decorators.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Python Decorators
88
</base-title>
99

1010
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.
1212

1313
## Bare bone decorator
1414

@@ -182,9 +182,10 @@ say_hi("James")
182182

183183
## Relevant links
184184

185+
- <router-link to="/blog/python-decorators-for-beginners">Python Decorators: Simple Patterns to Level Up Your Code</router-link>
186+
- <router-link to="/blog/python-easy-args-kwargs">Python *args and **kwargs Made Easy</router-link>
185187
- <router-link to="/cheatsheet/functions">Functions</router-link>
186188
- <router-link to="/cheatsheet/args-and-kwargs">Args and Kwargs</router-link>
187-
- <router-link to="/blog/python-easy-args-kwargs">Python *args and **kwargs Made Easy</router-link>
188189
- <router-link to="/builtin/classmethod">classmethod()</router-link>
189190
- <router-link to="/builtin/staticmethod">staticmethod()</router-link>
190191
- <router-link to="/builtin/property">property()</router-link>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /