After a year of development significant release of the programming language . Python 3.9 became the first release after the project on of preparing and supporting releases. New significant releases will now be formed once a year, while corrective updates will be issued every two months. Each major branch will be supported for one and a half years, after which corrections for vulnerability fixes will be generated for another three and a half years.
Work on the new branch now starts five months before the release of the next branch, i.e., simultaneously with the release of Python 3.9 alpha testing of the Python 3.10 branch. The Python 3.10 branch will be in the alpha release stage for seven months, during which new features will be added and bugs will be fixed. After that, beta testing will be conducted for three months, during which new feature additions will be prohibited and all attention will be focused on fixing bugs. The last two months before the release will be spent in the release candidate stage, during which final stabilization will occur.
in Python 3.9:
- In dictionaries defined using the built-in dict class, support for merging operators ‘|’ and updating ‘|=’, which complement the methods {**d1, **d2} and dict.update previously proposed for merging dictionaries.
>>> x = {"key1": "value1 from x", "key2": "value2 from x"}
>>> y = {"key2": "value2 from y", "key3": "value3 from y"}>>> x | y
{'key1': 'value1 from x', 'key2': 'value2 from y', 'key3': 'value3 from y'}>>> y | x
{'key2': 'value2 from x', 'key3': 'value3 from y', 'key1': 'value1 from x'} - The built-in collection types include list, dict, and tuple, which can be used as base types without importing from the typing module. Thus, instead of typing.List, typing.Dict, and typing.Tuple, one can now specify
simply list, dict, and tuple:def greet_all(names: list[str]) -> None:
for name in names:
print("Hello", name) - flexible tools for annotating functions and variables. To attach annotations, a new type Annotated has been added to the typing module, extending existing types with additional metadata that can be used during static analysis or for runtime optimizations. To access the metadata from the code, a parameter include_extras has been added to the method typing.get_type_hints().
charType = Annotated[int, ctype("char")]
UnsignedShort = Annotated[int, struct2.ctype('H')] - Grammar requirements for decorators — any expression suitable for use in if and while blocks can now be used as a decorator. This change significantly improved the readability of PyQt5 code and simplified the maintenance of this module:
Previously:
button_0 = buttons[0]
@button_0.clicked.connectYou can now write:
@buttons[0].clicked.connect - In the standard library module , which includes information from the IANA time zone database.
>>> from zoneinfo import ZoneInfo
>>> from datetime import datetime, timedelta
>>> # Daylight Saving Time
>>> dt = datetime(2020, 10, 31, 12, tzinfo=ZoneInfo('America/Los_Angeles'))
>>> print(dt)
2020-10-31 12:00:00-07:00>>> dt.tzname()
'PDT'>>> # Standard Time
>>> dt += timedelta(days=7)
>>> print(dt)
2020-11-07 12:00:00-08:00>>> print(dt.tzname())
PST - The graphlib module has been added, which provides support for topological sorting of graphs.
- New methods for removing prefixes and suffixes from strings — str.removeprefix(prefix) and str.removesuffix(suffix). These methods have been added to the str, bytes, bytearray, and collections.UserString objects.
>>> s = 'FooBar'
>>> s.removeprefix('Foo')
'Bar' - a new parser (Parsing Expression Grammar), which replaces the parser. The use of the new parser has eliminated some 'hacks' used to bypass limitations in LL(1) and significantly reduced the maintenance costs of the parser. In terms of performance, the new parser is approximately at the same level as the previous one, but surpasses it in flexibility, allowing more freedom when designing new language features. The code for the old parser is still preserved and can be restored using the '-X oldparser' flag or the environment variable 'PYTHONOLDPARSER=1', but will be removed in release 3.10.
- The ability for C-extension methods to access the state of the modules in which they are defined, using direct pointer dereferencing instead of finding the module state using the PyState_FindModule function. This change enhances the performance of C modules by reducing or completely eliminating the overhead associated with checking the module state. To associate a module with a class, the C function PyType_FromModuleAndSpec() has been proposed, to obtain the module and its state the C functions PyType_GetModule() and PyType_GetModuleState(), and to provide the method with access to the class in which it is defined, the C function PyCMethod and the METH_METHOD flag.
- Garbage collector from blocking collections that include resurrected objects, which remain accessible externally after the finalizer is run.
- A method has been added , allowing the use of the Linux kernel subsystem "pidfd" to handle the situation of reusing PIDs (pidfd is linked to a specific process and does not change, while the PID may be tied to another process after the current process associated with this PID terminates).
- Support for the Unicode specification has been updated to version 13.0.0.
- Resolved when reinitializing the Python interpreter in a single process.
- Performance optimizations have been made for built-in types such as range, tuple, set, frozenset, list, and dict, by applying the fast call protocol Vectorcall for quicker access to objects written in C.
- The modules _abc, audioop, _bz2, _codecs, _contextvars, _crypt, _functools, _json, _locale, operator, resource, time, and _weakref have been transitioned to load with .
- The standard library modules audioop, ast, grp, _hashlib, pwd, _posixsubprocess, random, select, struct, termios, and zlib have been converted to use a limited , which resolves the compatibility issue for extension module builds across different Python versions (when upgrading versions, there is no need to rebuild extension modules, and modules compiled for 3.9 will work in the 3.10 branch).
- In the asyncio module, support for the reuse_address parameter has been discontinued due to potential security issues (using SO_REUSEADDR for UDP in Linux allows different processes to attach listening sockets to the UDP port).
- New optimizations have been added, such as improved performance for signal handlers in multithreaded applications, enhanced speed for the subprocess module in FreeBSD environments, and accelerated assignment of temporary variables (assigning a variable in the expression "for y in [expr]" is now on par with the performance of the expression "y = expr"). Overall, most tests show a decrease in performance compared to the 3.8 branch (speedup is only seen in the write_local and write_deque tests):
Python Version 3.4 3.5 3.6 3.7 3.8 3.9
————— — — — — — —Variable and attribute read access:
read_local 7.1 7.1 5.4 5.1 3.9 4.0
read_nonlocal 7.1 8.1 5.8 5.4 4.4 4.8
read_global 15.5 19.0 14.3 13.6 7.6 7.7
read_builtin 21.1 21.6 18.5 19.0 7.5 7.7
read_classvar_from_class 25.6 26.5 20.7 19.5 18.4 18.6
read_classvar_from_instance 22.8 23.5 18.8 17.1 16.4 20.1
read_instancevar 32.4 33.1 28.0 26.3 25.4 27.7
read_instancevar_slots 27.8 31.3 20.8 20.8 20.2 24.5
read_namedtuple 73.8 57.5 45.0 46.8 18.4 23.2
read_boundmethod 37.6 37.9 29.6 26.9 27.7 45.9Variable and attribute write access:
write_local 8.7 9.3 5.5 5.3 4.3 4.2
write_nonlocal 10.5 11.1 5.6 5.5 4.7 4.9
write_global 19.7 21.2 18.0 18.0 15.8 17.2
write_classvar 92.9 96.0 104.6 102.1 39.2 43.2
write_instancevar 44.6 45.8 40.0 38.9 35.5 40.7
write_instancevar_slots 35.6 36.1 27.3 26.6 25.7 27.7Data structure read access:
read_list 24.2 24.5 20.8 20.8 19.0 21.1
read_deque 24.7 25.5 20.2 20.6 19.8 21.6
read_dict 24.3 25.7 22.3 23.0 21.0 22.5
read_strdict 22.6 24.3 19.5 21.2 18.9 21.6Data structure write access:
write_list 27.1 28.5 22.5 21.6 20.0 21.6
write_deque 28.7 30.1 22.7 21.8 23.5 23.2
write_dict 31.4 33.3 29.3 29.2 24.7 27.8
write_strdict 28.4 29.9 27.5 25.2 23.1 29.8Stack (or queue) operations:
list_append_pop 93.4 112.7 75.4 74.2 50.8 53.9
deque_append_pop 43.5 57.0 49.4 49.2 42.5 45.5
deque_append_popleft 43.7 57.3 49.7 49.7 42.8 45.5Timing loop:
loop_overhead 0.5 0.6 0.4 0.3 0.3 0.3 - Many functions and methods in Python 2.7 that were previously marked as deprecated and raised a DeprecationWarning in the last release, including the unescape() method in html.parser.HTMLParser,
tostring() and fromstring() in array.array, isAlive() in threading.Thread, getchildren() and getiterator() in ElementTree, sys.getcheckinterval(), sys.setcheckinterval(), asyncio.Task.current_task(), asyncio.Task.all_tasks(), base64.encodestring() and base64.decodestring.
Source: opennet.ru
