Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created November 21, 2024 18:08
Show Gist options
  • Save akimboyko/af70a583a3aa7aaca82ae0648bbf3ecf to your computer and use it in GitHub Desktop.
Save akimboyko/af70a583a3aa7aaca82ae0648bbf3ecf to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "9bc5fb30-2308-49fc-bf63-6379d536cecb",
"metadata": {},
"source": [
"# Python 3.13"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "373ae7a8-c7e1-47ff-a6e1-db4e3c2f0cde",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.13.0 | packaged by Anaconda, Inc. | (main, Oct 7 2024, 16:40:03) [Clang 14.0.6 ]\n"
]
}
],
"source": [
"import sys\n",
"print(sys.version)"
]
},
{
"cell_type": "markdown",
"id": "c1b3ab59-fe7e-41cf-8f72-25d5286a4a78",
"metadata": {},
"source": [
"## REPL changes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3b6d7655-7ede-4e3f-a4c4-0ea4296d2acd",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'my_non_existing_var' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241m1\u001b[39m \u001b[38;5;241m+\u001b[39m \u001b[43mmy_non_existing_var\u001b[49m\n",
"\u001b[0;31mNameError\u001b[0m: name 'my_non_existing_var' is not defined"
]
}
],
"source": [
"1 + my_non_existing_var"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a5678483-8154-4761-bc3d-9685dbe8e419",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'ellipsis'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;241;43m1\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'ellipsis'"
]
}
],
"source": [
"1 + ..."
]
},
{
"cell_type": "markdown",
"id": "4890ff1e-c7c3-479e-b004-474bb2bb18c1",
"metadata": {},
"source": [
"```\n",
">>> 1 + my_non_existing_var\n",
"Traceback (most recent call last):\n",
" File \"<python-input-0>\", line 1, in <module>\n",
" 1 + my_non_existing_var\n",
" ^^^^^^^^^^^^^^^^^^^\n",
"NameError: name 'my_non_existing_var' is not defined\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "748a1033-de9e-4c17-972c-79526d691920",
"metadata": {},
"source": [
"## `deprecated` decorator"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "cf3eca63-c3d7-45d0-b968-a51b398a95d0",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/_6/0y2346cj603d9lhdb2w6nvgm0000gp/T/ipykernel_780/1488851572.py:7: DeprecationWarning: should not be used anymore\n",
" some_function(\"Bob\")\n"
]
},
{
"data": {
"text/plain": [
"'hello Bob'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from warnings import deprecated\n",
"\n",
"@deprecated(\"should not be used anymore\")\n",
"def some_function(name):\n",
" return f\"hello {name}\"\n",
"\n",
"some_function(\"Bob\")"
]
},
{
"cell_type": "markdown",
"id": "f627bd85-1531-448e-86b7-70f4a2b46222",
"metadata": {},
"source": [
"## `__static_attributes__`\n",
"Mostly for tools and data classes"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7bf6ba8d-bc00-45ae-b227-8b63f5b970a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('db_name',)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Client():\n",
" host: str = \"127.0.0.1\"\n",
" port: int = 8888\n",
" def __init__(self, db_name: str):\n",
" self.db_name = db_name\n",
"\n",
"Client.__static_attributes__"
]
},
{
"cell_type": "markdown",
"id": "fb49f345-6fc7-4faf-8737-4d912c66501e",
"metadata": {},
"source": [
"## Pattern Matching Enhancements\n",
"Pattern matching, introduced in Python 3.10, has been significantly improved."
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "b6366c25-16e7-4d06-ac82-97a5d10b40db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Both values are positive\n",
"At least one value is negative\n",
"Unknown data\n"
]
}
],
"source": [
"from collections import namedtuple\n",
"\n",
"Container = namedtuple(\"Container\", [\"x\", \"y\"])\n",
"\n",
"\n",
"def handle_data(data: Container) -> str:\n",
" match data:\n",
" case (x, y) if x > 0 and y > 0:\n",
" return \"Both values are positive\"\n",
" case (x, y) if x < 0 or y < 0:\n",
" return \"At least one value is negative\"\n",
" case _:\n",
" return \"Unknown data\"\n",
"\n",
"print(handle_data(Container(x=1, y=2)))\n",
"print(handle_data(Container(x=1, y=-1)))\n",
"print(handle_data(\"a\"))"
]
},
{
"cell_type": "markdown",
"id": "319680a1-2d4b-48e2-9334-3bc30e76646f",
"metadata": {},
"source": [
"## ~`freeze` support~\n",
"\n",
"Finally not implemented, see https://github.com/diegojromerolopez/gelidum"
]
},
{
"cell_type": "markdown",
"id": "0901d555-0cd7-42b4-b650-7c5bfcb7df0e",
"metadata": {},
"source": [
"## `gc.collect()` and circular references\n",
"Python 3.13 improves garbage collection (GC) by reducing memory overhead in long-running processes, making it particularly useful in web servers, data pipelines, and large-scale applications.\n",
"\n",
"See https://github.com/python/cpython/issues/100403"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "20648051-2bea-48f4-9129-1e52ea9094fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"before: 711\n",
"after: 0\n",
"after: 0\n",
"after: 3\n"
]
}
],
"source": [
"import gc\n",
"\n",
"from dataclasses import dataclass\n",
"\n",
"@dataclass\n",
"class Node:\n",
" id_value: int\n",
" left: \"Node | None\" = None\n",
" right: \"Node | None\" = None\n",
"\n",
"node1 = Node(1)\n",
"node2 = Node(2)\n",
"node3 = Node(3)\n",
"\n",
"node1.right = node2\n",
"node2.right = node3\n",
"node3.right = node1\n",
"\n",
"node1.left = node3\n",
"node3.left = node2\n",
"node2.left = node1\n",
"\n",
"print(f\"before: {gc.collect()}\")\n",
"\n",
"# del node1, node2, node3\n",
"\n",
"del node1\n",
"print(f\"after: {gc.collect()}\")\n",
"\n",
"del node2\n",
"print(f\"after: {gc.collect()}\")\n",
"\n",
"del node3\n",
"print(f\"after: {gc.collect()}\")"
]
},
{
"cell_type": "markdown",
"id": "31331edc-dbf7-4835-b619-15f147669766",
"metadata": {},
"source": [
"## An experimental just-in-time (JIT) compiler\n",
"\n",
"https://docs.python.org/3/whatsnew/3.13.html#an-experimental-just-in-time-jit-compiler\n",
"\n",
"## Free-threaded CPython\n",
"\n",
"`python 3.13t`\n",
"\n",
"CPython now has experimental support for running in a free-threaded mode, with the global interpreter lock (GIL) disabled.\n",
"\n",
"- https://docs.python.org/3/whatsnew/3.13.html#whatsnew313-free-threaded-cpython\n",
"- https://github.com/lip234/python_313_benchmark\n",
"- https://github.com/ArjanCodes/examples/blob/main/2024/python_3_13/gil_test.py -- GIL benchmark"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "3ca23945-cf30-4c78-8d66-f861b6f02619",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 3.13.0\n",
" 3.13.0t\n",
" 3.13-dev\n",
" 3.13t-dev\n",
" pypy2.7-7.3.13-src\n",
" pypy2.7-7.3.13\n",
" pypy3.9-7.3.13-src\n",
" pypy3.9-7.3.13\n",
" pypy3.10-7.3.13-src\n",
" pypy3.10-7.3.13\n"
]
}
],
"source": [
"!pyenv install -l | grep 3.13"
]
},
{
"cell_type": "markdown",
"id": "663a508b-c2ee-4906-a0a7-0fa60b73afc6",
"metadata": {},
"source": [
"## Release schedule\n",
"\n",
"Next Python 3.XY versions to be reseased annually on October"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2df6fd95-4dce-4812-b8aa-9ed06a0f6822",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment