Skip to content

Latest commit

 

History

History

python-312

Python 3.12 Demos

This repository holds example code that demos some of the new features in Python 3.12.

Introduction

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:

You'll find examples from these tutorials in this repository.

Examples

This section only contains brief instructions on how you can run the examples. See the tutorials for technical details.

Improved Error Messages

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.

Support For the Linux perf Profiler

Setting Up

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

Using perf With Python

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

Rendering Flame Graphs

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.

New F-String Implementation and Syntax Formalization

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.

Static Typing Improvements

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.

Type Variables and Generic Classes, Functions, and Type Aliases

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:

Additionally, typed_queue.py shows the implementation of typed queues without using type variables.

Modeling Inheritance With @override

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.

Annotating **kwargs With Typed Dictionaries

The file options.py shows how you can use a typed dictionary to annotate variable keyword arguments.

Inlined Comprehensions

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.

Group Iterables Into Batches With itertools.batched()

Run quarters.py for an example showing how to group months into quarters.

List Files and Directories With Path.walk()

Run pathlib_walk.py to compare .walk() and .rglob() for listing files and directories.

Authors

License

Distributed under the MIT license. See LICENSE for more information.