diff options
| author | Simeon Simeonov | 2022-11-07 05:50:13 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2022-11-07 05:50:13 +0100 |
| commit | f52fe43ba3bea5e9f5091445513279f800494760 (patch) | |
| tree | 1312ba70576c90c622e563a6d6d2790c70a878fd | |
| parent | 2dfe3da941ea5d0be8102eaceb77f12c37991869 (diff) | |
Update notebooks/python/python_3_8_to_3_11.ipynb and notebooks/python/python_oo.ipynb
| -rw-r--r-- | notebooks/python/python_3_8_to_3_11.ipynb | 345 | ||||
| -rw-r--r-- | notebooks/python/python_oo.ipynb | 10 |
2 files changed, 270 insertions, 85 deletions
diff --git a/notebooks/python/python_3_8_to_3_11.ipynb b/notebooks/python/python_3_8_to_3_11.ipynb index 52d5630..62fea7d 100644 --- a/notebooks/python/python_3_8_to_3_11.ipynb +++ b/notebooks/python/python_3_8_to_3_11.ipynb | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | "id": "95c6942c-cd6a-437f-a35e-f6bbaf3555bd", | 5 | "id": "95c6942c-cd6a-437f-a35e-f6bbaf3555bd", |
| 6 | "metadata": {}, | 6 | "metadata": {}, |
| 7 | "source": [ | 7 | "source": [ |
| 8 | "# A very brief summary of changes introduced in Python 3.8, 3.9, 3.10 and 3.11\n" | 8 | "# A very brief summary of changes introduced in Python 3.8, 3.9, 3.10 and 3.11" |
| 9 | ] | 9 | ] |
| 10 | }, | 10 | }, |
| 11 | { | 11 | { |
| @@ -31,7 +31,7 @@ | |||
| 31 | }, | 31 | }, |
| 32 | { | 32 | { |
| 33 | "cell_type": "code", | 33 | "cell_type": "code", |
| 34 | "execution_count": 226, | 34 | "execution_count": 113, |
| 35 | "id": "a21e461a-8a7c-49d2-8a53-ab63ff2488bb", | 35 | "id": "a21e461a-8a7c-49d2-8a53-ab63ff2488bb", |
| 36 | "metadata": {}, | 36 | "metadata": {}, |
| 37 | "outputs": [ | 37 | "outputs": [ |
| @@ -40,10 +40,7 @@ | |||
| 40 | "output_type": "stream", | 40 | "output_type": "stream", |
| 41 | "text": [ | 41 | "text": [ |
| 42 | "List is too long (6 elements, expected <= 10)\n", | 42 | "List is too long (6 elements, expected <= 10)\n", |
| 43 | "List is too long (6 elements, expected <= 10)\n", | 43 | "List is too long (6 elements, expected <= 10)\n" |
| 44 | "my_list[3]='Eric'\n", | ||
| 45 | "my_list[4]='Terry J'\n", | ||
| 46 | "my_list[5]=Michael\n" | ||
| 47 | ] | 44 | ] |
| 48 | } | 45 | } |
| 49 | ], | 46 | ], |
| @@ -59,18 +56,12 @@ | |||
| 59 | "\n", | 56 | "\n", |
| 60 | "# while using the walrus operator...\n", | 57 | "# while using the walrus operator...\n", |
| 61 | "if (list_length := len(my_list)) > 5:\n", | 58 | "if (list_length := len(my_list)) > 5:\n", |
| 62 | " print(f\"List is too long ({list_length} elements, expected <= 10)\")\n", | 59 | " print(f\"List is too long ({list_length} elements, expected <= 10)\")" |
| 63 | "\n", | ||
| 64 | "\n", | ||
| 65 | "# f-strings support for self-documenting expressions and debugging using =\n", | ||
| 66 | "print(f\"{my_list[3]=!r}\")\n", | ||
| 67 | "print(f\"{my_list[4]=}\") # !r is implicit\n", | ||
| 68 | "print(f\"{my_list[5]=!s}\")" | ||
| 69 | ] | 60 | ] |
| 70 | }, | 61 | }, |
| 71 | { | 62 | { |
| 72 | "cell_type": "code", | 63 | "cell_type": "code", |
| 73 | "execution_count": 227, | 64 | "execution_count": 114, |
| 74 | "id": "99b75989-e991-4f77-a62e-01776f5b606c", | 65 | "id": "99b75989-e991-4f77-a62e-01776f5b606c", |
| 75 | "metadata": {}, | 66 | "metadata": {}, |
| 76 | "outputs": [ | 67 | "outputs": [ |
| @@ -114,6 +105,29 @@ | |||
| 114 | ] | 105 | ] |
| 115 | }, | 106 | }, |
| 116 | { | 107 | { |
| 108 | "cell_type": "code", | ||
| 109 | "execution_count": 115, | ||
| 110 | "id": "f683072f-c555-404b-b2c9-0d7d3621b118", | ||
| 111 | "metadata": {}, | ||
| 112 | "outputs": [ | ||
| 113 | { | ||
| 114 | "name": "stdout", | ||
| 115 | "output_type": "stream", | ||
| 116 | "text": [ | ||
| 117 | "my_list[3]='Eric'\n", | ||
| 118 | "my_list[4]='Terry J'\n", | ||
| 119 | "my_list[5]=Michael\n" | ||
| 120 | ] | ||
| 121 | } | ||
| 122 | ], | ||
| 123 | "source": [ | ||
| 124 | "# f-strings support for self-documenting expressions and debugging using =\n", | ||
| 125 | "print(f\"{my_list[3]=!r}\")\n", | ||
| 126 | "print(f\"{my_list[4]=}\") # !r is implicit\n", | ||
| 127 | "print(f\"{my_list[5]=!s}\")" | ||
| 128 | ] | ||
| 129 | }, | ||
| 130 | { | ||
| 117 | "cell_type": "markdown", | 131 | "cell_type": "markdown", |
| 118 | "id": "1801f5cb-6620-43c9-a1c8-1ada25b046bf", | 132 | "id": "1801f5cb-6620-43c9-a1c8-1ada25b046bf", |
| 119 | "metadata": {}, | 133 | "metadata": {}, |
| @@ -151,7 +165,7 @@ | |||
| 151 | }, | 165 | }, |
| 152 | { | 166 | { |
| 153 | "cell_type": "code", | 167 | "cell_type": "code", |
| 154 | "execution_count": 228, | 168 | "execution_count": 116, |
| 155 | "id": "a21fdf0a-92d2-4987-93c4-ea68f34f71b3", | 169 | "id": "a21fdf0a-92d2-4987-93c4-ea68f34f71b3", |
| 156 | "metadata": {}, | 170 | "metadata": {}, |
| 157 | "outputs": [ | 171 | "outputs": [ |
| @@ -161,7 +175,15 @@ | |||
| 161 | "text": [ | 175 | "text": [ |
| 162 | "2019\n", | 176 | "2019\n", |
| 163 | "('GREHAM', 'John', 'TerryG', 'Eric', 'TerryJ', 'Michael')\n", | 177 | "('GREHAM', 'John', 'TerryG', 'Eric', 'TerryJ', 'Michael')\n", |
| 164 | "9.333333333333334\n", | 178 | "Performing a very expensive operation\n", |
| 179 | "Performing a very expensive operation\n", | ||
| 180 | "dataset.string='836278421', id(dataset.string)=36514294704\n", | ||
| 181 | "Performing a very expensive operation\n", | ||
| 182 | "Performing a very expensive operation\n", | ||
| 183 | "dataset.string='836278421', id(dataset.string)=36514289520\n", | ||
| 184 | "Performing a very expensive operation\n", | ||
| 185 | "dataset.cached_string='836278421', id(dataset.cached_string)=36514289520\n", | ||
| 186 | "dataset.cached_string='836278421', id(dataset.cached_string)=36514289520\n", | ||
| 165 | "Permutations of 10 things taken 3 at a time: math.perm(10, 3)=720\n", | 187 | "Permutations of 10 things taken 3 at a time: math.perm(10, 3)=720\n", |
| 166 | "Combinations of 10 things taken 3 at a time: math.comb(10, 3)=120\n" | 188 | "Combinations of 10 things taken 3 at a time: math.comb(10, 3)=120\n" |
| 167 | ] | 189 | ] |
| @@ -172,7 +194,7 @@ | |||
| 172 | "import re\n", | 194 | "import re\n", |
| 173 | "\n", | 195 | "\n", |
| 174 | "notice = \"Copyright © 2019\"\n", | 196 | "notice = \"Copyright © 2019\"\n", |
| 175 | "copyright_year_pattern = re.compile(r\"\\N{copyright sign}\\s*(\\d{4})\")\n", | 197 | "copyright_year_pattern = re.compile(r\"\\N{copyright sign}\\s*(\\d{4})\") # expands to the named Unicode character\n", |
| 176 | "print(copyright_year_pattern.search(notice).group(1))\n", | 198 | "print(copyright_year_pattern.search(notice).group(1))\n", |
| 177 | "\n", | 199 | "\n", |
| 178 | "\n", | 200 | "\n", |
| @@ -187,11 +209,11 @@ | |||
| 187 | "# functools.lru_cache() can now be used as a straight decorator rather than as a function returning a decorator\n", | 209 | "# functools.lru_cache() can now be used as a straight decorator rather than as a function returning a decorator\n", |
| 188 | "import functools\n", | 210 | "import functools\n", |
| 189 | "\n", | 211 | "\n", |
| 190 | "@functools.lru_cache\n", | 212 | "@functools.lru_cache(maxsize=256)\n", |
| 191 | "def f(x):\n", | 213 | "def f(x):\n", |
| 192 | " pass\n", | 214 | " pass\n", |
| 193 | "\n", | 215 | "\n", |
| 194 | "@functools.lru_cache(maxsize=256)\n", | 216 | "@functools.lru_cache # Python >= 3.8 (maxsize will be 128)\n", |
| 195 | "def f(x):\n", | 217 | "def f(x):\n", |
| 196 | " pass\n", | 218 | " pass\n", |
| 197 | "\n", | 219 | "\n", |
| @@ -200,15 +222,26 @@ | |||
| 200 | "import statistics\n", | 222 | "import statistics\n", |
| 201 | "\n", | 223 | "\n", |
| 202 | "class Dataset:\n", | 224 | "class Dataset:\n", |
| 203 | " def __init__(self, sequence_of_numbers):\n", | 225 | " def __init__(self, sequence_of_numbers):\n", |
| 204 | " self.data = sequence_of_numbers\n", | 226 | " self.data = sequence_of_numbers\n", |
| 227 | "\n", | ||
| 228 | " @property\n", | ||
| 229 | " def string(self) -> str:\n", | ||
| 230 | " return self._stringify()\n", | ||
| 205 | "\n", | 231 | "\n", |
| 206 | " @functools.cached_property\n", | 232 | " @functools.cached_property\n", |
| 207 | " def variance(self):\n", | 233 | " def cached_string(self) -> str:\n", |
| 208 | " return statistics.variance(self.data)\n", | 234 | " return self._stringify()\n", |
| 209 | "\n", | 235 | "\n", |
| 210 | "dataset = Dataset((8, 2, 4))\n", | 236 | " def _stringify(self) -> str:\n", |
| 211 | "print(dataset.variance)\n", | 237 | " print(\"Performing a very expensive operation\")\n", |
| 238 | " return \"\".join(map(str, self.data))\n", | ||
| 239 | "\n", | ||
| 240 | "dataset = Dataset((836, 278, 421))\n", | ||
| 241 | "print(f\"{dataset.string=}, {id(dataset.string)=}\")\n", | ||
| 242 | "print(f\"{dataset.string=}, {id(dataset.string)=}\")\n", | ||
| 243 | "print(f\"{dataset.cached_string=}, {id(dataset.cached_string)=}\")\n", | ||
| 244 | "print(f\"{dataset.cached_string=}, {id(dataset.cached_string)=}\")\n", | ||
| 212 | "\n", | 245 | "\n", |
| 213 | "\n", | 246 | "\n", |
| 214 | "# new combinatoric functions math.perm() and math.comb()\n", | 247 | "# new combinatoric functions math.perm() and math.comb()\n", |
| @@ -247,7 +280,7 @@ | |||
| 247 | }, | 280 | }, |
| 248 | { | 281 | { |
| 249 | "cell_type": "code", | 282 | "cell_type": "code", |
| 250 | "execution_count": 229, | 283 | "execution_count": 117, |
| 251 | "id": "1f567f26-3783-4435-bcff-48826ea50404", | 284 | "id": "1f567f26-3783-4435-bcff-48826ea50404", |
| 252 | "metadata": {}, | 285 | "metadata": {}, |
| 253 | "outputs": [ | 286 | "outputs": [ |
| @@ -256,8 +289,7 @@ | |||
| 256 | "output_type": "stream", | 289 | "output_type": "stream", |
| 257 | "text": [ | 290 | "text": [ |
| 258 | "{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}\n", | 291 | "{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}\n", |
| 259 | "{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}\n", | 292 | "{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}\n" |
| 260 | "Bravely bold Sir Robin rode forth fro\n" | ||
| 261 | ] | 293 | ] |
| 262 | } | 294 | } |
| 263 | ], | 295 | ], |
| @@ -273,12 +305,27 @@ | |||
| 273 | "\n", | 305 | "\n", |
| 274 | "x |= y\n", | 306 | "x |= y\n", |
| 275 | "\n", | 307 | "\n", |
| 276 | "print(f\"{x}\")\n", | 308 | "print(f\"{x}\")" |
| 277 | "\n", | 309 | ] |
| 278 | "\n", | 310 | }, |
| 311 | { | ||
| 312 | "cell_type": "code", | ||
| 313 | "execution_count": 118, | ||
| 314 | "id": "3f527469-64c7-4cef-8a4d-ef536c418fa1", | ||
| 315 | "metadata": {}, | ||
| 316 | "outputs": [ | ||
| 317 | { | ||
| 318 | "name": "stdout", | ||
| 319 | "output_type": "stream", | ||
| 320 | "text": [ | ||
| 321 | "Bravely bold Sir Robin rode forth fro\n" | ||
| 322 | ] | ||
| 323 | } | ||
| 324 | ], | ||
| 325 | "source": [ | ||
| 279 | "# PEP 616 – String methods to remove prefixes and suffixes\n", | 326 | "# PEP 616 – String methods to remove prefixes and suffixes\n", |
| 280 | "\n", | 327 | "\n", |
| 281 | "print(\"Bravely bold Sir Robin rode forth from Camelot\".removesuffix(\"m Camelot\"))\n" | 328 | "print(\"Bravely bold Sir Robin rode forth from Camelot\".removesuffix(\"m Camelot\"))" |
| 282 | ] | 329 | ] |
| 283 | }, | 330 | }, |
| 284 | { | 331 | { |
| @@ -335,9 +382,59 @@ | |||
| 335 | }, | 382 | }, |
| 336 | { | 383 | { |
| 337 | "cell_type": "code", | 384 | "cell_type": "code", |
| 338 | "execution_count": 230, | 385 | "execution_count": 119, |
| 339 | "id": "11aaff48-28a3-49f5-86b6-059af6ab534d", | 386 | "id": "11aaff48-28a3-49f5-86b6-059af6ab534d", |
| 340 | "metadata": {}, | 387 | "metadata": {}, |
| 388 | "outputs": [], | ||
| 389 | "source": [ | ||
| 390 | "# Parenthesized context managers are now officially allowed\n", | ||
| 391 | "import io\n", | ||
| 392 | "\n", | ||
| 393 | "with (\n", | ||
| 394 | " io.open(\"input_file.dat\", \"rb\") as input_fp,\n", | ||
| 395 | " io.open(\"output_file.dat\", \"wb\") as output_fp,\n", | ||
| 396 | "):\n", | ||
| 397 | " data = input_fp.read()\n", | ||
| 398 | " # do some processing magic on data\n", | ||
| 399 | " output_fp.write(data)" | ||
| 400 | ] | ||
| 401 | }, | ||
| 402 | { | ||
| 403 | "cell_type": "markdown", | ||
| 404 | "id": "208a032f-c0f1-4591-8dbb-3d10e080c29f", | ||
| 405 | "metadata": {}, | ||
| 406 | "source": [ | ||
| 407 | "### Better error messages:\n", | ||
| 408 | "\n", | ||
| 409 | "```Python\n", | ||
| 410 | "the_pythons = [\"Greham\", \"John\", \"Terry G\", \"Eric\",\n", | ||
| 411 | " \"Terry J\", \"Michael\"\n", | ||
| 412 | "some_other_code = foo()\n", | ||
| 413 | "```\n", | ||
| 414 | "\n", | ||
| 415 | "Python < 3.10:\n", | ||
| 416 | "\n", | ||
| 417 | "```Python\n", | ||
| 418 | " File \"test.py\", line 16\n", | ||
| 419 | " some_other_code = foo()\n", | ||
| 420 | " ^\n", | ||
| 421 | "SyntaxError: invalid syntax\n", | ||
| 422 | "```\n", | ||
| 423 | "\n", | ||
| 424 | "Python >= 3.10:\n", | ||
| 425 | "```Python\n", | ||
| 426 | " Cell In [25], line 15\n", | ||
| 427 | " the_pythons = [\"Greham\", \"John\", \"Terry G\", \"Eric\",\n", | ||
| 428 | " ^\n", | ||
| 429 | "SyntaxError: '[' was never closed\n", | ||
| 430 | "```" | ||
| 431 | ] | ||
| 432 | }, | ||
| 433 | { | ||
| 434 | "cell_type": "code", | ||
| 435 | "execution_count": 120, | ||
| 436 | "id": "59329225-db92-4eb6-ad1b-b88455e48d50", | ||
| 437 | "metadata": {}, | ||
| 341 | "outputs": [ | 438 | "outputs": [ |
| 342 | { | 439 | { |
| 343 | "name": "stdout", | 440 | "name": "stdout", |
| @@ -352,45 +449,12 @@ | |||
| 352 | "True" | 449 | "True" |
| 353 | ] | 450 | ] |
| 354 | }, | 451 | }, |
| 355 | "execution_count": 230, | 452 | "execution_count": 120, |
| 356 | "metadata": {}, | 453 | "metadata": {}, |
| 357 | "output_type": "execute_result" | 454 | "output_type": "execute_result" |
| 358 | } | 455 | } |
| 359 | ], | 456 | ], |
| 360 | "source": [ | 457 | "source": [ |
| 361 | "# Parenthesized context managers are now officially allowed\n", | ||
| 362 | "import io\n", | ||
| 363 | "\n", | ||
| 364 | "with (\n", | ||
| 365 | " io.open(\"input_file.dat\", \"rb\") as input_fp,\n", | ||
| 366 | " io.open(\"output_file.dat\", \"wb\") as output_fp,\n", | ||
| 367 | "):\n", | ||
| 368 | " data = input_fp.read()\n", | ||
| 369 | " # do some processing magic on data\n", | ||
| 370 | " output_fp.write(data)\n", | ||
| 371 | "\n", | ||
| 372 | "\n", | ||
| 373 | "# Better error messages:\n", | ||
| 374 | "\n", | ||
| 375 | "# the_pythons = [\"Greham\", \"John\", \"Terry G\", \"Eric\",\n", | ||
| 376 | "# \"Terry J\", \"Michael\"\n", | ||
| 377 | "# some_other_code = foo()\n", | ||
| 378 | "\n", | ||
| 379 | "# Results in:\n", | ||
| 380 | "\n", | ||
| 381 | "# Python >= 3.10\n", | ||
| 382 | "# Cell In [25], line 15\n", | ||
| 383 | "# the_pythons = [\"Greham\", \"John\", \"Terry G\", \"Eric\",\n", | ||
| 384 | "# ^\n", | ||
| 385 | "# SyntaxError: '[' was never closed\n", | ||
| 386 | "\n", | ||
| 387 | "# Python < 3.10\n", | ||
| 388 | "# File \"test.py\", line 16\n", | ||
| 389 | "# some_other_code = foo()\n", | ||
| 390 | "# ^\n", | ||
| 391 | "# SyntaxError: invalid syntax\n", | ||
| 392 | "\n", | ||
| 393 | "\n", | ||
| 394 | "# PEP 634, PEP 635, PEP 636 - Structural pattern matching\n", | 458 | "# PEP 634, PEP 635, PEP 636 - Structural pattern matching\n", |
| 395 | "command = \"start the clock\"\n", | 459 | "command = \"start the clock\"\n", |
| 396 | "# command = input(\"Command: \")\n", | 460 | "# command = input(\"Command: \")\n", |
| @@ -458,7 +522,7 @@ | |||
| 458 | "\n", | 522 | "\n", |
| 459 | "- The first reference implementation (CPython) using C11 instead of C89\n", | 523 | "- The first reference implementation (CPython) using C11 instead of C89\n", |
| 460 | "\n", | 524 | "\n", |
| 461 | "- Python 3.11 is between 10-60% faster than Python 3.10. On average, we measured a 1.25x speedup on the standard benchmark suite\n", | 525 | "- Python 3.11 is between 10-60% faster than Python 3.10. \"On average, we measured a 1.25x speedup on the standard benchmark suite\"\n", |
| 462 | "\n", | 526 | "\n", |
| 463 | "- **PEP 654** - Exception groups and except* - enable a program to raise and handle multiple unrelated exceptions simultaneously\n", | 527 | "- **PEP 654** - Exception groups and except* - enable a program to raise and handle multiple unrelated exceptions simultaneously\n", |
| 464 | "\n", | 528 | "\n", |
| @@ -474,8 +538,83 @@ | |||
| 474 | ] | 538 | ] |
| 475 | }, | 539 | }, |
| 476 | { | 540 | { |
| 541 | "cell_type": "markdown", | ||
| 542 | "id": "978a4c1e-9cc0-4117-ae8a-e85bcf9f5359", | ||
| 543 | "metadata": {}, | ||
| 544 | "source": [ | ||
| 545 | "## Faster CPython\n", | ||
| 546 | "\n", | ||
| 547 | "Many of the ideas presented are a result of the work on \"better error messages\" (Python 3.10) and \"fine-grained error locations in tracebacks\".\n", | ||
| 548 | "\n", | ||
| 549 | "\n", | ||
| 550 | "### Frozen imports / Static code objects\n", | ||
| 551 | "\n", | ||
| 552 | "Python caches bytecode in the `__pycache__` directory to speed up module loading.\n", | ||
| 553 | "\n", | ||
| 554 | "Selected set of core modules essential for Python startup are statically allocated by the interpreter (\"frozen\").\n", | ||
| 555 | "\n", | ||
| 556 | "Interpreter startup is now 10-15% faster in Python 3.11. This has a big impact for short-running programs using Python.\n", | ||
| 557 | "\n", | ||
| 558 | "\n", | ||
| 559 | "### Cheaper / optimized lazy Python frames\n", | ||
| 560 | "\n", | ||
| 561 | "Python frames are created whenever Python calls a Python function. This frame holds execution information. The following are new frame optimizations:\n", | ||
| 562 | "\n", | ||
| 563 | "- streamlined the frame creation process.\n", | ||
| 564 | "\n", | ||
| 565 | "- avoided memory allocation by generously re-using frame space on the C stack.\n", | ||
| 566 | "\n", | ||
| 567 | "- streamlined the internal frame struct to contain only essential information. Frames previously held extra debugging and memory management information.\n", | ||
| 568 | "\n", | ||
| 569 | "3-7% speedup.\n", | ||
| 570 | "\n", | ||
| 571 | "\n", | ||
| 572 | "### Inlined Python function calls\n", | ||
| 573 | "\n", | ||
| 574 | "Most Python function calls now consume no C stack space. This speeds up most of such calls. In simple recursive functions like fibonacci or factorial, a 1.7x speedup was observed. This also means recursive functions can recurse significantly deeper (if the user increases the recursion limit).\n", | ||
| 575 | "\n", | ||
| 576 | "1-3% improvement in pyperformance.\n", | ||
| 577 | "\n", | ||
| 578 | "\n", | ||
| 579 | "### PEP 659 – Specializing Adaptive Interpreter\n", | ||
| 580 | "\n", | ||
| 581 | "Any instruction that would benefit from specialization will be replaced by an \"adaptive\" form of that instruction. When executed, the adaptive instructions will specialize themselves in response to the types and values that they see. This process is known as \"quickening\".\n", | ||
| 582 | "\n", | ||
| 583 | "Once an instruction in a code object has executed enough times, that instruction will be \"specialized\" by replacing it with a new instruction that is expected to execute faster for that operation.\n", | ||
| 584 | "\n", | ||
| 585 | "\n", | ||
| 586 | "#### Quickening\n", | ||
| 587 | "\n", | ||
| 588 | "Quickening is the process of replacing slow instructions with faster variants.\n", | ||
| 589 | "\n", | ||
| 590 | "Quickened code has number of advantages over immutable bytecode:\n", | ||
| 591 | "\n", | ||
| 592 | "- it can be changed at runtime\n", | ||
| 593 | "\n", | ||
| 594 | "- it can use super-instructions that span lines and take multiple operands\n", | ||
| 595 | "\n", | ||
| 596 | "- it does not need to handle tracing as it can fallback to the original bytecode for that\n", | ||
| 597 | "\n", | ||
| 598 | "In order that tracing can be supported, the quickened instruction format should match the immutable, user visible, bytecode format: 16-bit instructions of 8-bit opcode followed by 8-bit operand.\n", | ||
| 599 | "\n", | ||
| 600 | "Each instruction that would benefit from specialization is replaced by an adaptive version during quickening.\n", | ||
| 601 | "\n", | ||
| 602 | "\n", | ||
| 603 | "#### Compatibility\n", | ||
| 604 | "\n", | ||
| 605 | "There will be no change to the language, library or API.\n", | ||
| 606 | "\n", | ||
| 607 | "The only way that users will be able to detect the presence of the new interpreter is through timing execution, the use of debugging tools, or measuring memory use.\n", | ||
| 608 | "\n", | ||
| 609 | "\n", | ||
| 610 | "#### Costs\n", | ||
| 611 | "\n", | ||
| 612 | "Memory, complexity (simeons)" | ||
| 613 | ] | ||
| 614 | }, | ||
| 615 | { | ||
| 477 | "cell_type": "code", | 616 | "cell_type": "code", |
| 478 | "execution_count": 231, | 617 | "execution_count": 121, |
| 479 | "id": "ef9285ea-2bdb-4fb0-968d-833011fe0724", | 618 | "id": "ef9285ea-2bdb-4fb0-968d-833011fe0724", |
| 480 | "metadata": {}, | 619 | "metadata": {}, |
| 481 | "outputs": [ | 620 | "outputs": [ |
| @@ -486,8 +625,7 @@ | |||
| 486 | "KeyError detected\n", | 625 | "KeyError detected\n", |
| 487 | "TypeError detected\n", | 626 | "TypeError detected\n", |
| 488 | "KeyError detected\n", | 627 | "KeyError detected\n", |
| 489 | "TypeError detected\n", | 628 | "TypeError detected\n" |
| 490 | "bad type - ['Really bad types at work']\n" | ||
| 491 | ] | 629 | ] |
| 492 | } | 630 | } |
| 493 | ], | 631 | ], |
| @@ -515,9 +653,24 @@ | |||
| 515 | " result = my_dict[key]\n", | 653 | " result = my_dict[key]\n", |
| 516 | " except* (KeyError, TypeError) as eg:\n", | 654 | " except* (KeyError, TypeError) as eg:\n", |
| 517 | " for e in eg.exceptions:\n", | 655 | " for e in eg.exceptions:\n", |
| 518 | " print(f\"{type(e).__name__} detected\")\n", | 656 | " print(f\"{type(e).__name__} detected\")" |
| 519 | " \n", | 657 | ] |
| 520 | "\n", | 658 | }, |
| 659 | { | ||
| 660 | "cell_type": "code", | ||
| 661 | "execution_count": 122, | ||
| 662 | "id": "c70ce21d-bdf8-4a30-a5a2-c888ab75b92a", | ||
| 663 | "metadata": {}, | ||
| 664 | "outputs": [ | ||
| 665 | { | ||
| 666 | "name": "stdout", | ||
| 667 | "output_type": "stream", | ||
| 668 | "text": [ | ||
| 669 | "bad type - ['Really bad types at work']\n" | ||
| 670 | ] | ||
| 671 | } | ||
| 672 | ], | ||
| 673 | "source": [ | ||
| 521 | "# PEP 678 – Enriching exceptions with notes\n", | 674 | "# PEP 678 – Enriching exceptions with notes\n", |
| 522 | "# BaseException gains a new method .add_note(note: str).\n", | 675 | "# BaseException gains a new method .add_note(note: str).\n", |
| 523 | "# If note is a string, .add_note(note) appends it to the __notes__ list, creating the attribute if it does not already exist.\n", | 676 | "# If note is a string, .add_note(note) appends it to the __notes__ list, creating the attribute if it does not already exist.\n", |
| @@ -530,7 +683,7 @@ | |||
| 530 | " e.add_note(\"Really bad types at work\")\n", | 683 | " e.add_note(\"Really bad types at work\")\n", |
| 531 | " raise\n", | 684 | " raise\n", |
| 532 | "except Exception as e:\n", | 685 | "except Exception as e:\n", |
| 533 | " print(f\"{e!s} - {e.__notes__}\")\n" | 686 | " print(f\"{e!s} - {e.__notes__}\")" |
| 534 | ] | 687 | ] |
| 535 | }, | 688 | }, |
| 536 | { | 689 | { |
| @@ -550,12 +703,22 @@ | |||
| 550 | "\n", | 703 | "\n", |
| 551 | "- added non parallel-safe `contextlib.chdir()` context manager to change the current working directory and then restore it on exit. Simple wrapper around `chdir()`\n", | 704 | "- added non parallel-safe `contextlib.chdir()` context manager to change the current working directory and then restore it on exit. Simple wrapper around `chdir()`\n", |
| 552 | "\n", | 705 | "\n", |
| 553 | "- added `datetime.UTC`, a convenience alias for `datetime.timezone.utc`" | 706 | "- added `datetime.UTC`, a convenience alias for `datetime.timezone.utc`\n", |
| 707 | "\n", | ||
| 708 | "- many chages and additions to the `enum` module\n", | ||
| 709 | "\n", | ||
| 710 | "- added `math.exp2(x)` (returns 2 raised to the power of x) and `math.cbrt(x)` (returns the cube root of x). `math.nan` is now always available (C11)\n", | ||
| 711 | "\n", | ||
| 712 | "- on Windows, `os.urandom()` now uses *BCryptGenRandom()*, instead of *CryptGenRandom()* which is deprecated\n", | ||
| 713 | "\n", | ||
| 714 | "- `pathlib.Path.glob()` and `pathlib.Path.rglob()` return only directories if pattern ends with a pathname components separator: `os.sep` or `os.altsep`\n", | ||
| 715 | "\n", | ||
| 716 | "- `time.sleep()` uses higher resulution 10E-6 seconds -> 10E-9 seconds (Unix), 10E-3 -> 10E-7 (Windows >= 8.1)\n" | ||
| 554 | ] | 717 | ] |
| 555 | }, | 718 | }, |
| 556 | { | 719 | { |
| 557 | "cell_type": "code", | 720 | "cell_type": "code", |
| 558 | "execution_count": 232, | 721 | "execution_count": 123, |
| 559 | "id": "f1f9977c-20f4-4bc1-9fa7-14acd0f16193", | 722 | "id": "f1f9977c-20f4-4bc1-9fa7-14acd0f16193", |
| 560 | "metadata": {}, | 723 | "metadata": {}, |
| 561 | "outputs": [ | 724 | "outputs": [ |
| @@ -568,9 +731,7 @@ | |||
| 568 | "3\n", | 731 | "3\n", |
| 569 | "3\n", | 732 | "3\n", |
| 570 | "4\n", | 733 | "4\n", |
| 571 | "5\n", | 734 | "5\n" |
| 572 | "0.0\n", | ||
| 573 | "+0.0\n" | ||
| 574 | ] | 735 | ] |
| 575 | } | 736 | } |
| 576 | ], | 737 | ], |
| @@ -582,7 +743,25 @@ | |||
| 582 | "for i in *t1, *t2:\n", | 743 | "for i in *t1, *t2:\n", |
| 583 | " print(i)\n", | 744 | " print(i)\n", |
| 584 | "\n", | 745 | "\n", |
| 585 | "\n", | 746 | "\n" |
| 747 | ] | ||
| 748 | }, | ||
| 749 | { | ||
| 750 | "cell_type": "code", | ||
| 751 | "execution_count": 124, | ||
| 752 | "id": "7260ad15-4215-4b48-b6a3-870a14d8079c", | ||
| 753 | "metadata": {}, | ||
| 754 | "outputs": [ | ||
| 755 | { | ||
| 756 | "name": "stdout", | ||
| 757 | "output_type": "stream", | ||
| 758 | "text": [ | ||
| 759 | "0.0\n", | ||
| 760 | "+0.0\n" | ||
| 761 | ] | ||
| 762 | } | ||
| 763 | ], | ||
| 764 | "source": [ | ||
| 586 | "# PEP 682 – Format specifier for signed zero\n", | 765 | "# PEP 682 – Format specifier for signed zero\n", |
| 587 | "# When z is present, negative zero (whether the original value or the result of rounding) will be normalized to positive zero\n", | 766 | "# When z is present, negative zero (whether the original value or the result of rounding) will be normalized to positive zero\n", |
| 588 | "import decimal\n", | 767 | "import decimal\n", |
| @@ -591,7 +770,7 @@ | |||
| 591 | "print(f\"{x:z.1f}\")\n", | 770 | "print(f\"{x:z.1f}\")\n", |
| 592 | "\n", | 771 | "\n", |
| 593 | "x = decimal.Decimal('-.00001')\n", | 772 | "x = decimal.Decimal('-.00001')\n", |
| 594 | "print(f\"{x:+z.1f}\")\n" | 773 | "print(f\"{x:+z.1f}\")" |
| 595 | ] | 774 | ] |
| 596 | } | 775 | } |
| 597 | ], | 776 | ], |
diff --git a/notebooks/python/python_oo.ipynb b/notebooks/python/python_oo.ipynb index 9f0774a..fe717a9 100644 --- a/notebooks/python/python_oo.ipynb +++ b/notebooks/python/python_oo.ipynb | |||
| @@ -735,7 +735,7 @@ | |||
| 735 | "In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation. Also defined as deriving new classes (sub classes) from existing ones such as super class or base class and then forming them into a hierarchy of classes.\n", | 735 | "In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based inheritance), retaining similar implementation. Also defined as deriving new classes (sub classes) from existing ones such as super class or base class and then forming them into a hierarchy of classes.\n", |
| 736 | "\n", | 736 | "\n", |
| 737 | "In most class-based object-oriented languages, an object created through inheritance, a \"child object\", acquires all the properties and behaviors of the \"parent object\" , with the exception of: constructors, destructor, overloaded operators and friend functions of the base class. \n", | 737 | "In most class-based object-oriented languages, an object created through inheritance, a \"child object\", acquires all the properties and behaviors of the \"parent object\" , with the exception of: constructors, destructor, overloaded operators and friend functions of the base class. \n", |
| 738 | "Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces.\n", | 738 | "Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces. It is a \"is-a\" relationship between base (parent) class and a derived (child) class.\n", |
| 739 | "\n", | 739 | "\n", |
| 740 | "One can simply view inheritance as a tool for code reuse.\n", | 740 | "One can simply view inheritance as a tool for code reuse.\n", |
| 741 | "\n", | 741 | "\n", |
| @@ -1048,7 +1048,13 @@ | |||
| 1048 | "\n", | 1048 | "\n", |
| 1049 | "Not all classes admit a linearization. There are cases, in complicated hierarchies, where it is not possible to derive a class such that its linearization respects all the desired properties. \n", | 1049 | "Not all classes admit a linearization. There are cases, in complicated hierarchies, where it is not possible to derive a class such that its linearization respects all the desired properties. \n", |
| 1050 | "`TypeError: Cannot create a consistent method resolution order (MRO) for bases X, Y` is raised then.\n", | 1050 | "`TypeError: Cannot create a consistent method resolution order (MRO) for bases X, Y` is raised then.\n", |
| 1051 | "\n" | 1051 | "\n", |
| 1052 | "\n", | ||
| 1053 | "## The Liskov substitution principle (LSP):\n", | ||
| 1054 | "\n", | ||
| 1055 | "Functions that use references to base class objects must be able to use objects of derived classes without knowing it.\n", | ||
| 1056 | "\n", | ||
| 1057 | "In other words: an object (such as a class) may be replaced by a sub-object (such as a class that extends the first class) without breaking the program.\n" | ||
| 1052 | ] | 1058 | ] |
| 1053 | }, | 1059 | }, |
| 1054 | { | 1060 | { |
