Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created November 21, 2024 18:07
Show Gist options
  • Save akimboyko/7bdc6a24c5742cae031d6fea9f13ba6d to your computer and use it in GitHub Desktop.
Save akimboyko/7bdc6a24c5742cae031d6fea9f13ba6d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "c7a7e374-f7ff-4355-8313-6ad78a148260",
"metadata": {},
"source": [
"# Python 3.12"
]
},
{
"cell_type": "markdown",
"id": "51addbd8-958e-4e59-8778-197f11efd188",
"metadata": {},
"source": [
"## Improved error messaging"
]
},
{
"cell_type": "markdown",
"id": "ccfb41c1-8884-4107-9f14-ceaa122d5404",
"metadata": {},
"source": [
"```python\n",
"sys.version_info\n",
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1, in <module>\n",
"NameError: name 'sys' is not defined. Did you forget to import 'sys'?\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "ff19e26c-9895-4a3f-813f-6da4b9d17f21",
"metadata": {},
"source": [
"```python\n",
"import a.y.z from b.y.z\n",
"Traceback (most recent call last):\n",
" File \"<stdin>\", line 1\n",
" import a.y.z from b.y.z\n",
" ^^^^^^^^^^^^^^^^^^^^^^^\n",
"SyntaxError: Did you mean to use 'from ... import ...' instead?\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "e22cbf24-3a5b-485c-a4c4-299b37482261",
"metadata": {},
"source": [
"## Immortal _immutable_ objects\n",
"\n",
"Mostly C API, not yet fully availalbe from Python code\n",
"\n",
"- https://peps.python.org/pep-0683/\n",
"- https://engineering.fb.com/2023/08/15/developer-tools/immortal-objects-for-python-instagram-meta/\n",
"\n",
"This is an important building block towards a multi-core Python runtime. "
]
},
{
"cell_type": "markdown",
"id": "bb55385e-f05b-4c47-9c90-e1130c5b40b6",
"metadata": {},
"source": [
"## Nested f-string"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2f3e49e4-23e7-4110-84cf-3279a6fb8c66",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"literals: A; B; C\n",
"literals: A; B; C\n"
]
}
],
"source": [
"some_literals = [\"A\", \"B\", \"C\"]\n",
"\n",
"# before\n",
"print(f\"literals: {'; '.join(some_literals)}\")\n",
"\n",
"# now\n",
"print(f\"literals: {\"; \".join(some_literals)}\")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "efd61d72-c7df-4389-8311-363e2789e261",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f\"{f\"{f\"{f\"{f\"{f\"{1+1}\"}\"}\"}\"}\"}\""
]
},
{
"cell_type": "markdown",
"id": "9217c03e-cc4b-4ab7-90a1-ecce99ed21f9",
"metadata": {},
"source": [
"## `kwargs` unpacked to specify type"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "48b36e32-7c94-4104-a00e-f3fde87d9aab",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 'A', 'b': 'B'}\n"
]
}
],
"source": [
"from typing import Unpack\n",
"\n",
"def print_all(**kwargs: Unpack[str]):\n",
" print(kwargs)\n",
"\n",
"print_all(a=\"A\", b=\"B\")"
]
},
{
"cell_type": "markdown",
"id": "6b3adf96-9194-4051-9365-3c3b975d45aa",
"metadata": {},
"source": [
"## `override` keyword"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "b5702a4f-8062-41a4-adf3-66ab2c964faa",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'file'"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from typing import override\n",
"from typing import List\n",
"\n",
"\n",
"class BaseHandler:\n",
" def load_snapshot(self) -> str: pass\n",
"\n",
"class DatabaseHandler(BaseHandler):\n",
" @override\n",
" def load_snapshot(self) -> str:\n",
" return \"db\"\n",
"\n",
"class FileHandler(BaseHandler):\n",
" @override\n",
" def load_snapshots(self) -> List[str]: # Error reported by type checker\n",
" return \"file\""
]
},
{
"cell_type": "markdown",
"id": "6a0df028-3a31-4576-96fe-34d29d872729",
"metadata": {},
"source": [
"## Type Parameter Syntax\n",
"\n",
"```python\n",
"type Point[T] = tuple[T, T]\n",
"type Constainer[T] = list[T] | set[T]\n",
"\n",
"def max[T](args: Iterable[T]) -> T:\n",
" ...\n",
"\n",
"class list[T]:\n",
" def __getitem__(self, index: int, /) -> T:\n",
" ...\n",
"\n",
" def append(self, element: T) -> None:\n",
" ...\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6096f401-ce0f-4012-9c7e-0ac426c033cb",
"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