-
Notifications
You must be signed in to change notification settings - Fork 4
Python changes since 3.8 (summary)
Jerry Morrison edited this page Sep 30, 2022
·
5 revisions
Python 3.8.10
Supports macOS 11.0 (Big Sur) and Apple Silicon.
Python 3.8.14
A security improvement in int
<-> str
conversions.
Python 3.9 to 3.9.14
-
dict
union operatord1 | d2
,d1 |= d2
. - Type hinting allowed standard collection types, e.g.
list[str]
. - Any valid expression can now be used as a decorator.
-
str.removeprefix(prefix)
andstr.removesuffix(suffix)
methods. -
PEP 593, Flexible function and variable annotations.
typing.Annotated
type to decorate existing types with context-specific metadata. - Add
os.pidfd_open()
which allows process management without races and signals. - CPython now uses a PEG parser.
- The IANA Time Zone Database is now in the standard library in the zoneinfo module.
- New
zoneinfo
(time zone) module andgraphlib
module (topo-sort). -
math.gcd()
now handles multiple arguments. - Added
random.Random.randbytes()
.
- builtins (
range
,tuple
,set
,frozenset
,list
,dict
, ...) use vectorcall; - modules (
_abc
,audioop
,_bz2
,_codecs
,_contextvars
,_crypt
,_functools
,_json
,_locale
,math
,operator
,resource
,time
,_weakref
) use multiphase initialization; - fast access to module state from methods of C extension types;
- GC doesn't block on resurrected objects.
- => Use the
-W default
or-W error
CLI option to check for DeprecationWarning in your code since various Python 2 backward compatibility details are being removed after Python 3.9. (Warnings Filter can be used to ignore warnings from third-party code.) - => The
collections.*
aliases tocollections.abc.*
will be removed in Python 3.10. wcEcoli usescollections.Mapping
andcollections.Sequence
. -
__import__()
now raises ImportError instead of ValueError. - => The
__file__
attribute of the__main__
module is now an absolute path. - =>
"".replace("", s, n)
returnss
instead of an empty string for all non-zeron
, consistent with"".replace("", s)
. Ditto for bytes and bytearray objects. - => Unexpected errors in calling the
__iter__
method are no longer masked byTypeError
in thein
operator and functionscontains()
,indexOf()
, andcountOf()
of the operator module.
- After Python 3.9, the
random
module will restrict its seeds toNone
,int
,float
,str
,bytes
, andbytearray
values.
Python 3.10.7
- Structural Pattern Matching (match/case statements).
- Parenthesized context managers are now officially allowed.
- Better error messages.
-
zip()
now has an optional strict flag to require that all the iterables have an equal length. Precise line numbers for debugging and other tools. - PEP 604, Allow writing union types as
X | Y
in type hints,isinstance()
, andissubclass()
. - PEP 613, Explicit Type Aliases:
MyType: TypeAlias = "ClassName"; y: TypeAlias = ClassName
. - PEP 612, Parameter Specification Variables:
typing.ParamSpec
andtyping.Concatenate
. - PEP 647, User-Defined Type Guards.
- Add
int.bit_count()
method (the number of 1 bits). -
pprint.pprint()
now accepts a new underscore_numbers keyword argument and it can pretty-print dataclasses.dataclass instances. - Add
statistics.covariance()
, Pearson’s.correlation()
, and simple.linear_regression()
functions.
- => The distutils package is deprecated, to be removed in Python 3.12. Its functionality for specifying package builds has already been completely replaced by third-party packages setuptools and packaging, and most other commonly used APIs are available elsewhere in the standard library (such as platform, shutil, subprocess or sysconfig).
- Any numeric literal immediately followed by one of keywords
and
,else
,for
,if
,in
,is
, andor
since these can be ambiguous, e.g.0x1for
.
- Constructors
str()
,bytes()
, andbytearray()
are faster. - Python starts faster.
- Byte code speedups.
- Faster bz2 / lzma / zlib (de)compressors.
- When using stringized type annotations, code loads faster.
- Faster
str1 in str2
andstr2.find(str1)
. - Faster
map()
,filter()
,reversed()
,bool()
, andfloat()
via vectorcall.
Python 3.11
- PEP 654: Exception Groups,
except*
. - PEP 678: Enriching Exceptions with Notes.
- PEP 680: Add
tomllib
library to parse TOML files. - PEP 657: Fine grained error locations in tracebacks.
- New
-P
command line option andPYTHONSAFEPATH
environment variable to disable automatically prepending a potentially unsafe working dir or script directory tosys.path
. - PEP 646: Variadic generic type hints, e.g. NumPy arrays parameterized with the array shape.
- PEP 655: Type hints can declare individual
TypedDict
fields as required or optional. - PEP 673:
Self
type, a simple way to annotate a method as returning an instance of its class. - PEP 675:
LiteralString
type hint. - PEP 681:
@typing.dataclass_transform()
to decorate an annotation which transforms a class, like@dataclass()
does. -
__future__.annotations
is in limbo. - Add
object.__getstate__()
. - The format specifier
z
formats -0 (float or decimal) as 0, e.g.f'{-.00001:z.2f}'
. - Add
StrEnum
(the enum members must be strings),ReprEnum
,@member
,@global_enum
, and other enum features. - Add regex atomic grouping
(?>...)
and possessive quantifiers (*+
,++
,?+
,{m,n}+
). - Add
typing.assert_never()
andtyping.Never
, which are useful for asking a type checker to confirm that a line of code is unreachable. - Add
typing.reveal_type()
, which is useful for asking a type checker what type it inferred for an expression. - Add
typing.assert_type()
, which is useful for asking a type checker to confirm that the type inferred the expected type. - Add
TypedDict
andNamedTuple
classes can inherit fromGeneric[T]
. - Add
@typing.final
which marks a method or class as final, i.e. can't be overridden.
- 10-60% faster execution (avg 25% on the standard benchmark suite) than CPython 3.10 due to many improvements including adaptive bytecodes, "zero-cost" exceptions, faster startup, faster calls from Python code to Python functions (can also recurse deeper since it doesn't add to the C statck), faster regex matching. See https://docs.python.org/3.11/whatsnew/3.11.html#faster-cpython and https://docs.python.org/3.11/whatsnew/3.11.html#optimizations
- PEP 594: Removing modules from the standard library.
- PEP 624: Remove
Py_UNICODE
encoder APIs. - PEP 670: Convert macros to functions in the Python C API.
- PEP 594: The
chunk
module is deprecated and slated to be removed in version 3.13.tablereader.py
uses it. -
typing.Text
(for Python 2/3 compatibility).