This repository holds example code that demos some of the new features in Python 3.12.
You need Python 3.12 installed to run these examples. See the following tutorial instructions:
Note that for the perf
support, you'll need to build Python from source code with additional compiler flags enabled, as explained below.
You can learn more about Python 3.12's new features in the following Real Python tutorials:
- Python 3.12: Cool New Features for You to Try
- Python 3.12 Preview: Ever Better Error Messages
- Python 3.12 Preview: Support For the Linux
perf
Profiler - Python 3.12 Preview: More Intuitive and Consistent F-Strings
- Python 3.12 Preview: Subinterpreters
- Python 3.12 Preview: Static Typing Improvements
You'll find examples from these tutorials in this repository.
This section only contains brief instructions on how you can run the examples. See the tutorials for technical details.
Run encoder.py
to create an encoded message like the one shown in the tutorial. You can decode the message using decoder.py
.
You can swap the import statement to import d from this
in either of the files to encounter the improved error message:
>>> import d from this
File "<stdin>", line 1
import d from this
^^^^^^^^^^^^^^^^^^
SyntaxError: Did you mean to use 'from ... import ...' instead?
In local_self.py
, you can see a naive reproduction of another improved error message. Pick apart the example code to learn more about how this was implemented in Python 3.12. You can also run shapes.py
to see the self error message in action.
See Ever Better Error Messages in Python 3.12 for more information.
You'll need to download, build, and install Python 3.12 from the source code with the frame pointer optimizations disabled:
$ git clone --branch v3.12.0b2 https://github.com/python/cpython.git
$ cd cpython/
$ export CFLAGS='-fno-omit-frame-pointer -mno-omit-leaf-frame-pointer'
$ ./configure --prefix="$HOME/python-custom-build"
$ make -j $(nproc)
$ make install
Create and activate a new virtual environment based on Python 3.12:
$ "$HOME/python-custom-build/bin/python3" -m venv venv/ --prompt 'py3.12-custom'
$ source venv/bin/activate
Install dependencies from the requirements.txt
file into your virtual environment:
(py3.12-custom) $ python -m pip install -r requirements.txt
Record Samples:
$ cd perf-profiler/
$ sudo perf record -g -F max ../venv/bin/python -X perf benchmark.py
Display reports:
$ cd perf-profiler/
$ sudo perf report
$ sudo perf report --stdio -g
$ sudo perf report --hierarchy --verbose --call-graph fractal --sort sample,dso
Download Perl scripts and add them to your $PATH
environment variable:
$ git clone git@github.com:brendangregg/FlameGraph.git
$ export PATH="$(pwd)/FlameGraph:$PATH"
Generate the flame graph and save it to a local file:
$ cd perf-profiler/
$ sudo perf script | stackcollapse-perf.pl | flamegraph.pl > flamegraph.svg
Open the flame graph in your default SVG viewer: (Use your web browser for the interactive features.)
$ xdg-open flamegraph.svg
Produce a pure-Python flame graph by filtering and processing the collapsed stack traces:
$ sudo perf script | stackcollapse-perf.pl > traces.txt
$ cat traces.txt | python censor.py -m benchmark,PIL > traces_censored.txt
$ cat traces_censored.txt | flamegraph.pl --minwidth 10 > flamegraph.svg
See Support For the Linux perf
Profiler in Python 3.12 for more information.
Once you have 3.12 installed, open any file from the f-string/
folder. Uncomment the code as needed. Run the scripts from your command line or execute the code directly in a REPL session. You'll see the new f-string implementation in action.
For example, go ahead and run the following command:
$ python backslashes.py
Hello
World!
I
am
a
Pythonista!
In this example, you can see how the new implementation of f-strings allows you to include backslashes in embedded expressions. This wasn't possible with f-strings in earlier versions of Python.
You'll find all static typing examples inside the typing/
directory. You should install the Pyright type checker from PyPI:
$ python -m pip install pyright
You can then run type checks by running pyright
. For some features, you need to specify --pythonversion 3.12
.
You can find comparisons between the old and the new syntax for type variables in the following files, with the new 3.12 syntax shown in the commented part of the code:
generic_queue.py
generic_stack.py
list_helpers.py
concatenation.py
inspect_string.py
deck.py
alias.py
Additionally, typed_queue.py
shows the implementation of typed queues without using type variables.
The file quiz.py
shows how to use the new @override
decorator. In addition to the code in the tutorial, this file includes support for reading questions from files. This is done to show that @override
works well together with other decorators like @classmethod
.
Similarly, accounts.py
contains the bank account classes annotated with @override
. To play with the bank account code, you should run it interactively: python -i accounts.py
.
The file options.py
shows how you can use a typed dictionary to annotate variable keyword arguments.
You can run benchmarks on comprehensions by running comprehension_benchmark.py
. If you have multiple versions of Python, then you should run the benchmarks on all of them and compare the results.
Run quarters.py
for an example showing how to group months into quarters.
Run pathlib_walk.py
to compare .walk()
and .rglob()
for listing files and directories.
- Martin Breuss, E-mail: martin@realpython.com
- Bartosz Zaczyński, E-mail: bartosz@realpython.com
- Leodanis Pozo Ramos, E-mail: leodanis@realpython.com
- Geir Arne Hjelle, E-mail: geirarne@realpython.com
Distributed under the MIT license. See LICENSE
for more information.