grouped by versions (3.0 to 3.10).
most important is marked with bold.
(https://docs.python.org/3/whatsnew/3.0.html)
- binary data and Unicode
Python 3.0 uses the concepts of text and (binary) data.
All text is Unicode ("...")
b"..." literals for binary data.
any attempt to mix text and data in Python 3.0 raises TypeError
>The rules for translating a Unicode string into a sequence of bytes are called a character encoding, or just an encoding.
str.encode() to go from str to bytes, bytes.decode() to go from bytes to str
basestring was removed. Use str instead -- again, bytes are separate
Files opened as text files (still the default mode for open()) always use an encoding to map between strings (in memory) and bytes (on disk). Binary files (opened with a b in the mode argument) always use bytes in memory.
The default source encoding is now UTF-8.
Python’s re module defaults to the re.UNICODE flag rather than re.ASCII. This means, for instance, that r"\w" matches Unicode word characters, not just ASCII letters. - Exception chaining
raise [expr [from expr]]
- print()
- dict methods .keys(), .items() and .values() return “views” instead of lists
- removed dict.iter*() methods
- map() and filter() return iterators
- range() now behaves like xrange() used to behave
- 1/2 returns a float. Use 1//2 to get the truncating behavior.
- Octal literals 0720 -> 0o720
- Named parameters occurring after
*args
in the parameter list must be specified using keyword syntax in the call. You can also use a bare * in the parameter list to indicate that you don’t accept a variable-length argument list, but you do have keyword-only arguments. - Extended Iterable Unpacking e.g.
(a, *rest, b) = range(5)
- Set literals, e.g.
{1, 2}
, Set comprehensions - Tuple parameter unpacking removed. You can no longer write
def foo(a, (b, c)): ....
Usedef foo(a, b_c): b, c = b_c
instead. - Classic classes are gone.
- The io module is now the standard way of doing file I/O. The built-in open() function is now an alias for io.open()
- In Python 3.0, the accelerated versions are considered implementation details of the pure Python versions. Users should always import the standard version, which attempts to import the accelerated version and falls back to the pure Python version.
- You can now invoke super() without arguments
- (less important)
- BaseException
- The StringIO module has been turned into a class in the io module.
- The only acceptable syntax for relative imports is
from .[module] import name
. All import forms not starting with . are interpreted as absolute imports. (PEP 328) - Dictionary comprehensions (already in 2.7)
- new convention for specifying a metaclass
- Using nonlocal x you can now assign directly to a variable in an outer (but non-global) scope.
- long renamed to int
(https://docs.python.org/3/whatsnew/3.1.html)
- (less important)
- Directories and zip archives containing a
__main__.py
file can now be executed directly by passing their name to the interpreter. - The runpy module which supports the -m command line switch now supports the execution of packages by looking for and executing a
__main__
submodule when a package name is supplied.
- Directories and zip archives containing a
(https://docs.python.org/3/whatsnew/3.2.html)
- wsgi v1.0.1 https://docs.python.org/3/whatsnew/3.2.html#pep-3333-python-web-server-gateway-interface-v1-0-1
- (less important)
- concurrent.futures
- html.escape
- gil rewrite https://docs.python.org/3/whatsnew/3.2.html#multi-threading
- The QueueListener class, located in the logging.handlers
(https://docs.python.org/3/whatsnew/3.3.html)
- yield from https://stackoverflow.com/q/9708902/1183239
- venv
- OSError subclasses e.g. FileNotFoundError, ConnectionRefusedError
- datetime.timestamp()
- time.monotonic()
- clock_gettime(), perf_counter()
- (less important)
- signal.pthread_kill If signalnum is 0, then no signal is sent, but error checking is still performed; this can be used to check if the target thread is still running.
(https://docs.python.org/3/whatsnew/3.4.html)
- asyncio
- pathlib
- tracemalloc
- (less important)
- enum
- statistics
- gc.get_stats()
- multiprocessing now has an option to avoid using os.fork on Unix
- pickle protocol 4.
- regex.fullmatch()
- main_thread()
- Customization of CPython Memory Allocators
(https://docs.python.org/3/whatsnew/3.5.html)
- Type Hints
- additional unpacking generalizations
d3 = {**d1, **d2} print(*[1], *[2], 3, *[4, 5]) fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4}) [*range(4), 4] {'x': 1, **{'y': 2}}
- subprocess.run()
from __future__ import generator_stop
- Circular imports involving relative imports are now supported
(https://docs.python.org/3/whatsnew/3.6.html)
- f-strings / formatted string literals / Literal String Interpolation
f"He said his name is {repr(name)}."
- The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords
- 1_000_000
- (less important)
- Asynchronous Generators (-- asyncio)
- Asynchronous Comprehensions (-- asyncio)
- https://docs.python.org/3/whatsnew/3.6.html#pep-487-simpler-customization-of-class-creation
- https://docs.python.org/3/whatsnew/3.6.html#pep-487-descriptor-protocol-enhancements
- https://docs.python.org/3/whatsnew/3.6.html#pep-519-adding-a-file-system-path-protocol
- Preserving Class Attribute Definition Order
- Preserving Keyword Argument Order
- re Match object groups can be accessed by
__getitem__
, which is equivalent to group(). So mo['name'] is now equivalent to mo.group('name'). - sqlite3 no longer implicitly commits an open transaction before DDL statements.
- DTrace and SystemTap probing support has been added. https://docs.python.org/3/howto/instrumentation.html#instrumentation
- dict class gained the ability to remember insertion order (>As of Python 3.7, this is no longer an implementation detail and instead becomes a language feature)
- Circular imports involving absolute imports with binding a submodule to a name are now supported.
- dataclasses https://docs.python.org/3/library/dataclasses.html
- register_at_fork()
- (less important)
from __future__ import annotations
- Forced UTF-8 Runtime Mode https://docs.python.org/3/whatsnew/3.7.html#pep-540-forced-utf-8-runtime-mode https://vstinner.github.io/python37-new-utf8-mode.html
- Built-in breakpoint()
__getattr__()
on modules and will call it whenever a module attribute is otherwise not found.- New Time Functions With Nanosecond Resolution
- contextvars
- importlib.resources
- It is no longer possible to build Python with threading disabled.
- multiprocessing.shared_memory
- Assignment expressions (the “walrus operator”,
:=
) to assign values to variables as part of an expression. - (less important)
- Positional-only parameters
- Parallel filesystem cache for compiled bytecode files
- f'{cos(radians(theta))=:.3f}' expands to e.g. 'cos(radians(theta))=0.866'
- Runtime Audit Hooks
- Pickle protocol 5
- Dict and dictviews are now iterable in reversed insertion order using reversed()
- importlib.metadata module provides (provisional) support for reading metadata from third-party packages. For example, it can extract an installed package’s version number, list of entry points
- sys.unraisablehook()
- threading.excepthook()
- Merge (|) and update (|=) operators have been added to the built-in dict class.
- str.removeprefix(prefix) and str.removesuffix(suffix). Corresponding bytes, bytearray, and collections.UserString methods have also been added.
- Type Hinting Generics in Standard Collections
- zoneinfo, tzdata change
- Better error messages
- Structural Pattern Matching tutorial: https://www.python.org/dev/peps/pep-0636/
- typing: Type Union Operator
def square(number: int | float) -> int | float
, TypeAlias - https://docs.python.org/3.10/whatsnew/3.10.html#optional-encodingwarning-and-encoding-locale-option
- https://docs.python.org/3.10/whatsnew/3.10.html#pep-626-precise-line-numbers-for-debugging-and-other-tools
- zip() function now has an optional strict flag, used to require that all the iterables have an equal length.
- Using enclosing parentheses for continuation across multiple lines in context managers is now supported.
- contextlib.aclosing()
- dataclasses: Keyword-only fields
- stdlib tomllib: For parsing TOML
- >the interpreter will now point to the exact expression that caused the error
- good
typing
improvements https://docs.python.org/3.11/whatsnew/3.11.html#new-features-related-to-type-hints - Exception Groups and except* https://peps.python.org/pep-0654/
-
Python 3.11 is between 10-60% faster than Python 3.10.
- The add_note() method is added to BaseException. It can be used to enrich exceptions with context information that is not available at the time when the exception is raised.
-
Starred unpacking expressions can now be used in for statements.
- hashlib.file_digest()
- logging.getLevelNamesMapping()
- operator.call
- pathlib glob() and rglob() return only directories if pattern ends with a pathname components separator
- On Unix, time.sleep() now uses the clock_nanosleep() or nanosleep() function, if available
- significantly improved typing
- improved f-strings
- per-interpreter GIL >currently only available through the C-API, though a Python API is anticipated for 3.13
- new API for profilers, debuggers, and other tools ... you only pay for what you use, providing support for near-zero overhead debuggers and coverage tools
- buffer protocol accessible in Python
- Improved Error Messages
- CPython support for the Linux perf profiler
- itertools.batched()
- sqlite3 module can be invoked as a script, using the interpreter’s -m switch, in order to provide a simple SQLite shell
- uuid module can be executed as a script from the command line
- datetime: datetime.datetime’s utcnow() and utcfromtimestamp() are deprecated and will be removed in a future version. Instead, use timezone-aware objects to represent datetimes in UTC: respectively, call now() and fromtimestamp() with the tz parameter set to datetime.UTC.
- stop installing setuptools in environments created by venv
- Replace the builtin hashlib implementations of SHA1, SHA3, SHA2-384, SHA2-512, and MD5 with formally verified code from the HACL* project. These builtin implementations remain as fallbacks that are only used when OpenSSL does not provide them.