{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A PEP 622 Playground\n", "\n", "The purpose of this notebook is to facilitate interactive exploration of the proposed new syntax for _Structural Pattern Matching_ in Python 3.10, as described in [PEP-622](https://www.python.org/dev/peps/pep-0622).\n", "\n", "This notebook should be run with a kernel that uses a suitable version of Python with support for this PEP's new syntax. We start by making a quick sanity check that we're running at least version 3.10 of Python, if that's not the case you need to check your runtime environment.\n", "\n", "_Note:_ If you are new to Jupyter Notebooks, code cells (like the one below) can be executed by typing `Shift-Enter` inside the cell, or by using the \"play\" button (right-pointing triangle in the toolbar above)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import sys\n", "assert sys.version_info[:2] >= (3, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With that out of the way, let's introduce a very basic example that illustrates the syntax (and whether the cell below runs cleanly will be a good test that you have the right version of Python installed).\n", "\n", "We define a `dataclass` called `Point`, and a `whereis` function that matches points in different parts of the x-y plane:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from dataclasses import dataclass\n", "\n", "@dataclass\n", "class Point:\n", " x: int\n", " y: int\n", "\n", "def whereis(point):\n", " match point:\n", " case Point(0, 0):\n", " print(\"Origin\")\n", " case Point(0, y):\n", " print(f\"Y={y}\")\n", " case Point(x, 0):\n", " print(f\"X={x}\")\n", " case Point():\n", " print(\"Somewhere else\")\n", " case _:\n", " print(\"Not a point\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this definition we can now test various kinds of points:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X=1\n" ] } ], "source": [ "whereis(Point(1, 0))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Origin\n" ] } ], "source": [ "whereis(Point(0, 0))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Not a point\n" ] } ], "source": [ "whereis(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now start playing!\n", "\n", "You can explore these ideas further below. The `examples` directory contains some more sample code that you can run at the terminal (you can open a new terminal with the '+' icon or from the File menu), or you can try your own. The PEP contains full details on the syntax." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0a0" } }, "nbformat": 4, "nbformat_minor": 4 }