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: python-311/README.md
+61-76Lines changed: 61 additions & 76 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,12 +18,27 @@ You'll find examples from all these tutorials in this repository.
18
18
19
19
## Dependencies
20
20
21
-
Install necessary dependencies for the examples:
21
+
Create and activate a [virtual environment](https://realpython.com/python-virtual-environments-a-primer/):
22
22
23
23
```console
24
-
$ python -m pip install colorama parse
24
+
$ python -m venv venv
25
+
$ source venv/bin/activate
25
26
```
26
27
28
+
Install necessary dependencies for the examples (see [`requirements.in`](requirements.in)):
29
+
30
+
```console
31
+
(venv) $ python -m pip install colorama parse
32
+
```
33
+
34
+
Alternatively, you can install dependencies from [`requirements.txt`](requirements.txt) if you want to ensure that you're using the same versions of the third-party packages:
These examples have been run with [Python 3.11.0rc1](https://www.python.org/downloads/release/python-3110rc1/), the first release candidate of Python 3.11.
41
+
27
42
## Examples
28
43
29
44
This section only contains brief instructions on how you can run the examples. See the tutorials for technical details.
@@ -33,7 +48,7 @@ This section only contains brief instructions on how you can run the examples. S
33
48
Load [`scientists.py`](scientists.py) into your interactive REPL:
34
49
35
50
```console
36
-
$ python -i scientists.py
51
+
(venv) $ python -i scientists.py
37
52
```
38
53
You can then experiment with `dict_to_person()` and `convert_pair()`:
39
54
@@ -64,17 +79,12 @@ See [Even Better Error Messages in Python 3.11](https://realpython.com/python311
64
79
65
80
Use `ExceptionGroup` and `except*` to handle several errors at once:
+ Exception Group Traceback (most recent call last):
77
-
| ...
86
+
| File "/home/realpython/exception_group.py", line 2, in <module>
87
+
| raise ExceptionGroup(
78
88
| ExceptionGroup: group (2 sub-exceptions)
79
89
+-+---------------- 1 ----------------
80
90
| TypeError: str
@@ -90,7 +100,7 @@ See [Exception Groups and `except*` in Python 3.11](https://realpython.com/pytho
90
100
Run [`count.py`](count.py), [`count_gather.py`](count_gather.py), and [`count_taskgroup.py`](count_taskgroup.py) and compare their behaviors. For example:
[`tomllib_w.py`](tomllib_w.py)shows how you can write simplified TOML files:
139
+
You can use [`tomllib_w.py`](tomllib_w.py)to write simplified TOML files. [`write_toml.py`](write_toml.py) demonstrates how you can use it:
130
140
131
-
```pycon
132
-
>>> import tomllib_w
133
-
>>> data = {"url": "https://realpython.com/python311-tomllib/",
134
-
... "author": {"name": "Geir Arne Hjelle", "email": "[email protected]"}}
135
-
136
-
>>> print(tomllib_w.dumps(data))
141
+
```console
142
+
(venv) $ python write_toml.py
137
143
url = "https://realpython.com/python311-tomllib/"
138
144
139
145
[author]
140
146
name = "Geir Arne Hjelle"
141
-
email = "geirarne@realypython.com"
147
+
email = "geirarne@realpython.com"
142
148
```
143
149
144
150
See [`tomllib` TOML Parser in Python 3.11](https://realpython.com/python311-tomllib/#tomllib-toml-parser-in-python-311) and [PEP 680](https://peps.python.org/pep-0680/).
@@ -148,7 +154,7 @@ See [`tomllib` TOML Parser in Python 3.11](https://realpython.com/python311-toml
148
154
[`polar_point.py`](polar_point.py) uses `Self` for annotation:
149
155
150
156
```console
151
-
$ python polar_point.py
157
+
(venv) $ python polar_point.py
152
158
PolarPoint(r=5.0, φ=0.9272952180016122)
153
159
```
154
160
@@ -159,7 +165,7 @@ See [`Self` Type](https://realpython.com/python311-tomllib/#self-type) and [PEP
159
165
[`execute_sql.py`](execute_sql.py) shows an example of `LiteralString`:
160
166
161
167
```console
162
-
$ python execute_sql.py
168
+
(venv) $ python execute_sql.py
163
169
Pretending to execute: SELECT * FROM users
164
170
Pretending to execute: SELECT * FROM users
165
171
@@ -179,22 +185,18 @@ See [Variadic Generic Types](https://realpython.com/python311-tomllib/#variadic-
179
185
180
186
Use `.add_notes()` to annotate exceptions with custom notes:
181
187
182
-
```pycon
183
-
>>> err =ValueError(678)
184
-
>>> err.add_note("Enriching Exceptions with Notes")
185
-
>>> err.add_note("Python 3.11")
186
-
187
-
>>> err.__notes__
188
-
['Enriching Exceptions with Notes', 'Python 3.11']
189
-
>>> for note in err.__notes__:
190
-
... print(note)
191
-
...
188
+
```console
189
+
(venv) $ python exception_notes.py
190
+
err.__notes__ = ['Enriching Exceptions with Notes', 'Python 3.11']
191
+
192
+
--------------------- Loop over notes ----------------------
192
193
Enriching Exceptions with Notes
193
194
Python 3.11
194
195
195
-
>>> raise err
196
+
--------------- Notes are added to traceback ---------------
196
197
Traceback (most recent call last):
197
-
...
198
+
File "/home/realpython/exception_notes.py", line 12, in <module>
199
+
raise err
198
200
ValueError: 678
199
201
Enriching Exceptions with Notes
200
202
Python 3.11
@@ -206,14 +208,9 @@ See [Annotate Exceptions With Custom Notes](https://realpython.com/python311-exc
206
208
207
209
You can use `sys.exception()` to access the active exception:
208
210
209
-
```pycon
210
-
>>> import sys
211
-
212
-
>>> try:
213
-
... raiseValueError("bpo-46328")
214
-
... exceptValueError:
215
-
... print(f"Handling {sys.exception()}")
216
-
...
211
+
```console
212
+
(venv) $ python active_exception.py
213
+
Handling bpo-46328
217
214
Handling bpo-46328
218
215
```
219
216
@@ -226,7 +223,7 @@ See [Reference the Active Exception With `sys.exception()`](https://realpython.c
226
223
`traceback_demo.py` shows that tracebacks can be consistently accessed through the exception object:
@@ -237,30 +234,20 @@ See [Reference the Active Traceback Consistently](https://realpython.com/python3
237
234
238
235
You can use `math.cbrt()` to calculate cube roots:
239
236
240
-
```pycon
241
-
>>> import math
242
-
>>> math.cbrt(729)
243
-
9.000000000000002
244
-
245
-
>>> 729**(1/3)
246
-
8.999999999999998
247
-
248
-
>>> math.pow(729, 1/3)
249
-
8.999999999999998
237
+
```console
238
+
(venv) $ python cube_root.py
239
+
math.cbrt(729) = 9.000000000000002
240
+
729 ** (1 / 3) = 8.999999999999998
241
+
math.pow(729, 1 / 3) = 8.999999999999998
250
242
```
251
243
252
244
You can use `math.exp2()` to calculate powers of two:
253
245
254
-
```pycon
255
-
>>> import math
256
-
>>> math.exp2(16)
257
-
65536.0
258
-
259
-
>>> 2**16
260
-
65536
261
-
262
-
>>> math.pow(2, 16)
263
-
65536.0
246
+
```console
247
+
(venv) $ python power_of_two.py
248
+
math.exp2(16) = 65536.0
249
+
2**16 = 65536
250
+
math.pow(2, 16) = 65536.0
264
251
```
265
252
266
253
See [Cube Roots and Powers of Two](https://realpython.com/python311-error-messages/#cube-roots-and-powers-of-two), [BPO 44357](https://github.com/python/cpython/issues/88523) and [BPO 45917](https://github.com/python/cpython/issues/90075).
@@ -269,10 +256,9 @@ See [Cube Roots and Powers of Two](https://realpython.com/python311-error-messag
269
256
270
257
You can use underscores when defining fractions from strings:
271
258
272
-
```pycon
273
-
>>> from fractions import Fraction
274
-
>>> print(Fraction("6_024/1_729"))
275
-
6024/1729
259
+
```console
260
+
(venv) $ python underscore.py
261
+
Fraction('6_024/1_729') = 6024/1729
276
262
```
277
263
278
264
See [Underscores in Fractions](https://realpython.com/python311-error-messages/#underscores-in-fractions) and [BPO 44258](https://github.com/python/cpython/issues/88424).
@@ -281,13 +267,12 @@ See [Underscores in Fractions](https://realpython.com/python311-error-messages/#
281
267
282
268
The Norwegian calculator implemented in [`kalkulator.py`](kalkulator.py) uses `operator.call()`:
283
269
284
-
```pycon
285
-
>>> import kalkulator
286
-
>>> kalkulator.calculate("20 pluss 22")
287
-
42.0
288
-
289
-
>>> kalkulator.calculate("11 delt på 3")
290
-
3.6666666666666665
270
+
```console
271
+
(venv) $ python kalkulator.py
272
+
20 pluss 22 = 42.0
273
+
2022 minus 1991 = 31.0
274
+
45 ganger 45 = 2025.0
275
+
11 delt på 3 = 3.6666666666666665
291
276
```
292
277
293
278
See [Flexible Calling of Objects](https://realpython.com/python311-error-messages/#flexible-calling-of-objects) and [BPO 44019](https://github.com/python/cpython/issues/88185).
0 commit comments