The steering council has recently accepted PEP 680, which justifies the need for a TOML parser in Python’s standard library due to PEP 517’s move toward a new build system based on pyproject.toml. Until the feature freeze in Python 3.11 Beta 1, planned for May 2022, new features are continually discussed and added to the development branch of Python. That’s a whopping increase in performance! Naturally, the difference will vary depending on the task at hand, and you should generally expect less impressive numbers on average.įor more information on the performance optimizations, see the faster-cpython project on GitHub. $ SETUP = 'fib = lambda n: 1 if n < 2 else fib(n - 1) + fib(n - 2)' $ python3.10 -m timeit -s " $SETUP " 'fib(35)' 1 loop, best of 5: 3.16 sec per loop $ python3.11a5 -m timeit -s " $SETUP " 'fib(35)' 1 loop, best of 5: 1.96 sec per loopĪs you can see, it takes slightly over three seconds for Python 3.10 to complete the computation and less than two seconds for Python 3.11 Alpha 5. In particular, due to the circular reference problem, you can’t annotate an attribute or a method with the class that you’re defining: Apart from that, implementing fluent interface or data structures with cross-references, such as a tree or a linked list, would also benefit from this new type hint.īelow is a more concrete example that illustrates the usefulness of Self. There are many special methods in Python that return self. In short, you can use Self as a type hint, for example to annotate a method returning an instance of the class in which it was defined. If you’d like to get a taste of the Self type anyway, then use the latest commit in the main branch of CPython. PEP 673: Self TypeĮven though the release notes of Python 3.11 Alpha 5 mention this new feature, it hasn’t actually been merged to the main branch before tagging the release point in the repository’s history. + Exception Group Traceback (most recent call last): | File "/home/realpython/test.py", line 2, in | raise ExceptionGroup( | ^^^^^^^^^^^^^^^^^^^^^ | ExceptionGroup: Validation exceptions +-+- 1 - | ValueError: Invalid email +- 2 - | TypeError: Age must be a number +- 3 - | KeyError: 'No such country code' +-įor more information on exception groups and except*, see PEP 654. If you catch a regular exception with except*, then Python will bundle it in a temporary exception group for you. At the same time, there will be a new except* syntax for cherry-picking a particular member of an exception group while re-raising other members that you don’t currently want to deal with. But there are other use cases, like grouping multiple exceptions raised during data validation.īecause the exception group is going to be a standard exception, you’ll still be able to handle it using the regular try … except clause. The primary purpose of exception groups is to simplify error handling in concurrent code, especially Async IO, which has traditionally been verbose and messy. Python 3.11 will bring a new exception type called the exception group, which will allow for combining several exceptions into one container. > x, y, z = 1, 2, 0 > w = x / y / z Traceback (most recent call last):įile "", line 1, in ZeroDivisionError: float division by zeroįor more information on error locations in tracebacks, see PEP 657. Python 3.11.0a5 (main, Mar 1 2022, 10:05:02) on linux Type "help", "copyright", "credits" or "license" for more information.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |