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
Cloudflare Workers handlers are asynchronous, so understanding `await` is practical for fetch calls, bindings, and service interactions even when a small example uses `asyncio.sleep(0)` as a stand-in.
14
14
15
+
The alternative is ordinary `def` for work that completes immediately. Use async code for I/O-shaped waiting, not as a faster replacement for CPU-bound Python.
16
+
15
17
:::program
16
18
```python
17
19
import asyncio
@@ -86,4 +88,5 @@ asyncio.run(main())
86
88
- Calling an async function creates a coroutine object.
87
89
-`await` yields control until an awaitable completes.
88
90
- Workers request handlers are async, so this pattern appears around fetches and bindings.
91
+
- Prefer ordinary functions when there is no awaitable work to coordinate.
Copy file name to clipboardExpand all lines: src/example_sources/classes.md
+7-4Lines changed: 7 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@
2
2
slug = "classes"
3
3
title = "Classes"
4
4
section = "Classes"
5
-
summary = "Classes bundle data and behavior."
5
+
summary = "Classes bundle data and behavior into new object types."
6
6
doc_path = "/tutorial/classes.html"
7
7
+++
8
8
9
-
Classes define new object types by bundling data with behavior. They are useful when several values and operations belong together.
9
+
Classes define new object types by bundling data with behavior. They are useful when several values and operations belong together and should travel as one object.
10
10
11
-
`__init__` initializes each instance, and methods receive the instance as `self`. Assigning `self.value` stores state on that particular object.
11
+
The alternative is often a dictionary plus separate functions. That is fine for loose data, but a class gives the data a stable API and keeps behavior next to the state it changes.
12
12
13
-
Separate instances keep separate state. Mutating one `Counter` does not change another because each object has its own attributes.
13
+
`__init__` initializes each instance, and methods receive the instance as `self`. Separate instances keep separate state because each object has its own attributes.
14
14
15
15
:::program
16
16
```python
@@ -25,6 +25,8 @@ class Counter:
25
25
first = Counter()
26
26
second = Counter(10)
27
27
28
+
print(first.value)
29
+
print(second.value)
28
30
print(first.increment())
29
31
print(second.increment(5))
30
32
```
@@ -77,5 +79,6 @@ print(second.increment(5))
77
79
:::note
78
80
-`self` is the instance the method is operating on.
79
81
-`__init__` initializes each new object.
82
+
- Use classes when behavior belongs with state; use dictionaries for looser structured data.
80
83
- Instance attributes belong to one object, not to the class as a whole.
Copy file name to clipboardExpand all lines: src/example_sources/exception-groups.md
+28-12Lines changed: 28 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,19 +11,22 @@ see_also = [
11
11
]
12
12
+++
13
13
14
-
`ExceptionGroup` represents several unrelated exceptions raised together. `except*`handles the matching members of the group and lets other members continue separately.
14
+
`ExceptionGroup` represents several unrelated exceptions raised together. `except*`exists for code that may receive multiple failures at once, especially concurrent work.
15
15
16
-
This syntax is most useful with concurrent code, where several tasks can fail before the caller regains control.
16
+
Use ordinary `except` for one exception. Use `except*` only when the value being handled is an exception group and each matching subgroup needs its own handling.
17
17
18
-
Use ordinary `except` for one exception. Use `except*` only when the value being handled is an exception group.
18
+
Each `except*` clause receives a smaller exception group containing the matching exceptions.
19
19
20
20
:::program
21
21
```python
22
+
errors = ExceptionGroup(
23
+
"batch failed",
24
+
[ValueError("bad port"), TypeError("bad mode")],
25
+
)
26
+
print(len(errors.exceptions))
27
+
22
28
try:
23
-
raise ExceptionGroup(
24
-
"batch failed",
25
-
[ValueError("bad port"), TypeError("bad mode")],
26
-
)
29
+
raise errors
27
30
except*ValueErroras group:
28
31
print(type(group).__name__)
29
32
print(group.exceptions[0])
@@ -33,14 +36,27 @@ except* TypeError as group:
33
36
:::
34
37
35
38
:::cell
36
-
`ExceptionGroup` bundles several exception objects. `except* ValueError` receives a group containing only the matching `ValueError` members.
39
+
An exception group bundles several exception objects. This is different from an ordinary exception because more than one failure is present.
40
+
41
+
```python
42
+
errors = ExceptionGroup(
43
+
"batch failed",
44
+
[ValueError("bad port"), TypeError("bad mode")],
45
+
)
46
+
print(len(errors.exceptions))
47
+
```
48
+
49
+
```output
50
+
2
51
+
```
52
+
:::
53
+
54
+
:::cell
55
+
`except*` handles matching members of the group. The `ValueError` handler sees the value error, and the `TypeError` handler sees the type error.
Copy file name to clipboardExpand all lines: src/example_sources/exceptions.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,8 @@ Exceptions represent errors or unusual conditions that interrupt normal control
10
10
11
11
Keep the successful path separate from the recovery path. `else` runs only when no exception was raised, while `finally` runs either way for cleanup or bookkeeping.
12
12
13
+
Use exceptions when an operation cannot produce a valid result. Prefer ordinary conditionals for expected branches that are not errors.
14
+
13
15
Catch specific exceptions whenever possible. A broad catch can hide programming mistakes, while a targeted `ValueError` handler documents exactly what failure is expected.
0 commit comments