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
Expand broad-surface pages, trim see_also, fix doc_path anchor
Removes the scope_first_pass shortcut from every broad-title page that
took it. Each page now demonstrates the canonical forms its title
promises rather than scoping itself down with a link list.
Broad-page expansions
- literals: hex/binary/octal/underscore numeric literals; f-string
shown alongside raw and bytes literals.
- operators: bitwise & added with | and ^ to make the masking surface
complete.
- type-hints: union (`X | Y`), `Optional[X]`, and `TypeAlias` cells.
- testing: setUp fixture, assertEqual, and assertRaises in a single
TestCase. Runner output now reflects three test methods.
- async-await: async-with / async-for cell with an async context
manager and async generator, linking to async-iteration-and-context.
- packages: builds a temporary `shapes` package on disk to make
`from .submodule import name` and `__all__` concrete.
- regular-expressions: re.match vs re.search, re.compile, re.IGNORECASE
flag, and re.sub each in their own cell.
- special-methods: equality/hashing/ordering, container protocols,
callable, and context-manager dunders. The page is now a real tour.
Graph hygiene
- Trimmed see_also on literals, operators, inheritance-and-super,
special-methods, and type-hints to ≤4 entries so audit_example_graph
passes without warnings.
Doc-path correction
- bound-and-unbound-methods: doc_path was /reference/datamodel.html
#methods (no such anchor); corrected to #instance-methods.
Verification
- All 109 examples verify under Python 3.13.
- 39 unit tests pass.
- Quality checks all green: registry-integrity, confusable-pairs,
broad-surface-tours, footgun-coverage, notes-supported.
- score_examples reports every new page at 8.87 or higher (gate is 8.5).
- audit_example_graph --check exits 0.
- ruff, lint_seo_cache, format-examples --check, migration-parity all
pass. Golden fixture refreshed to reflect the new cells.
Copy file name to clipboardExpand all lines: src/example_sources/async-await.md
+60-1Lines changed: 60 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,6 @@ title = "Async Await"
4
4
section = "Async"
5
5
summary = "async def creates coroutines, and await pauses until awaitable work completes."
6
6
doc_path = "/library/asyncio-task.html"
7
-
scope_first_pass = true
8
7
see_also = [
9
8
"async-iteration-and-context",
10
9
"functions",
@@ -35,6 +34,30 @@ async def main():
35
34
print(titles)
36
35
37
36
asyncio.run(main())
37
+
38
+
39
+
classSession:
40
+
asyncdef__aenter__(self):
41
+
print("open")
42
+
returnself
43
+
44
+
asyncdef__aexit__(self, *_):
45
+
print("close")
46
+
returnFalse
47
+
48
+
49
+
asyncdefstream():
50
+
for slug in ["json", "datetime"]:
51
+
await asyncio.sleep(0)
52
+
yield slug
53
+
54
+
55
+
asyncdefdriver():
56
+
asyncwith Session():
57
+
asyncfor slug in stream():
58
+
print(slug)
59
+
60
+
asyncio.run(driver())
38
61
```
39
62
:::
40
63
@@ -90,6 +113,42 @@ asyncio.run(main())
90
113
```
91
114
:::
92
115
116
+
:::cell
117
+
`async with` and `async for` are the asynchronous forms of context managers and iteration. A class implements `__aenter__`/`__aexit__` to act as an async context manager; an `async def` function with `yield` becomes an async generator. The dedicated [async iteration and context](/iteration/async-iteration-and-context) page explains the protocols in depth.
118
+
119
+
```python
120
+
classSession:
121
+
asyncdef__aenter__(self):
122
+
print("open")
123
+
returnself
124
+
125
+
asyncdef__aexit__(self, *_):
126
+
print("close")
127
+
returnFalse
128
+
129
+
130
+
asyncdefstream():
131
+
for slug in ["json", "datetime"]:
132
+
await asyncio.sleep(0)
133
+
yield slug
134
+
135
+
136
+
asyncdefdriver():
137
+
asyncwith Session():
138
+
asyncfor slug in stream():
139
+
print(slug)
140
+
141
+
asyncio.run(driver())
142
+
```
143
+
144
+
```output
145
+
open
146
+
json
147
+
datetime
148
+
close
149
+
```
150
+
:::
151
+
93
152
:::note
94
153
- Calling an async function creates a coroutine object.
95
154
-`await` yields control until an awaitable completes.
String literals write Unicode text. Raw strings keep backslashes literal, and bytes literals write binary data rather than text.
77
+
Integer literals also accept hexadecimal (`0x`), binary (`0b`), and octal (`0o`) prefixes. Underscores group digits visually without changing the value.
78
+
79
+
```python
80
+
flags =0xFF
81
+
mask =0b1010
82
+
million =1_000_000
83
+
print(flags, mask, million)
84
+
```
85
+
86
+
```output
87
+
255 10 1000000
88
+
```
89
+
:::
90
+
91
+
:::cell
92
+
String literals write Unicode text. Raw strings keep backslashes literal, bytes literals write binary data rather than text, and f-strings (`f"..."`) embed expressions inline.
Bitwise operators work on integer bit patterns. They are useful for masks and flags, not ordinary boolean logic.
92
+
Bitwise operators work on integer bit patterns. They are useful for masks and flags, not ordinary boolean logic.`&` is bitwise AND, `|` is bitwise OR, `^` is exclusive OR, and `<<` shifts left.
withopen(os.path.join(pkg, "square.py"), "w") as square:
44
+
square.write("def area(side):\n return side * side\n")
45
+
sys.path.insert(0, tmp)
46
+
try:
47
+
import shapes
48
+
print(shapes.area(3))
49
+
print(shapes.__all__)
50
+
finally:
51
+
sys.path.remove(tmp)
52
+
sys.modules.pop("shapes", None)
53
+
sys.modules.pop("shapes.square", None)
33
54
```
34
55
:::
35
56
@@ -78,8 +99,41 @@ True
78
99
```
79
100
:::
80
101
102
+
:::cell
103
+
Inside a package's `__init__.py`, `from .submodule import name` re-exports a submodule's name at the package root, and `__all__` lists the names that `from package import *` should make visible. This cell builds a temporary `shapes` package on disk to make both forms concrete.
104
+
105
+
```python
106
+
import os
107
+
import sys
108
+
import tempfile
109
+
110
+
with tempfile.TemporaryDirectory() as tmp:
111
+
pkg = os.path.join(tmp, "shapes")
112
+
os.makedirs(pkg)
113
+
withopen(os.path.join(pkg, "__init__.py"), "w") as init:
0 commit comments