asyncio.TaskGroup
In previous versions, this was how to schedule a collection of async
tasks and await
on them.
|
|
Now we can simply enter into a TaskGroup context.
|
|
tomllib
In previous versions, we needed to use an external library to load and manipulate toml
formats. Now we can utilize tomllib straight out of the box.
|
|
contextlib.chdir
In previous versions, we had to keep a historic record to switch into a directory and back.
|
|
Now we can simply contextualize it.
|
|
typing.Self
In previous versions, we couldn’t directly reference the class as return type.
|
|
For example, we would get NameError
when running the following code.
|
|
Now we are able to reference using Self
without having to resort to a workaround of declaring a new TypeVar
or T
to represent the class.
|
|
functools.singledispatch
In previous versions, function overloading was not possible - in the conventional sense. We would “imitate” the overloading behavior by implementing enumerated function parameters and conditionally outputing the result. The downside is that this increases complexity of supposed “overloaded” function and keeping track of the logical bifurication.
|
|
Now we are able to utilize functools.singledispatch
and type signatures to behave similarly.
|
|
shutil.make_archive
In previous versions of shutil.make_archive
, we needed to temporarily switch the current working directory to archive file/directory (e.g. zip
, tar
, bztar
, xztar
, etc.) to its relative paths - similar to the following shell pattern:
|
|
Now we can just pass the new root_dir
argument.
tempfile.NamedTemporaryFile
’s delete_on_close
This is one of my favorite new features. In UNIX, the tempfile.NamedTemporaryFile
generates a random file under the /tmp
directory to facilitate ephemeral writes and reads (like caching). However, in the old days, we needed to keep track of the filenames if we ever wanted to manually delete them. With the new delete_on_close
boolean parameter, we can now offload this responsibility with .close()
.
os.PIDFD_NONBLOCK
In this version, a new flag to os.pidfd_open(pid: int, flags: int)
has been added to expose PIDFD_NONBLOCK
to Python (a.k.a. opening a file descriptor to a running process in a non-blocking mode).
|
|
Not sure what I’ll be using this for yet, but will be keeping it in mind.
math.sumprod
Instead of doing this:
|
|
We can just do this:
|
|
typing.Hashable
and typing.Sized
I use these type hint notations in my codebase - but with 3.12
, the naming convention has been changed to collections.abc.Hashable
and collections.abc.Sized
.
get_event_loop()
The asyncio.get_event_loop()
, which returns the event loop of the current async context, will now DeprecationWarning
if there is no context event loop.
typing.runtime_checkable
The runtime_checkable
decorator will now freeze the members of typing.Protocol
.
wstr
and wstr_length
From Unicode objects, the wstr
and wstr_length
have been removed. This reduces the object size by 8 or 16 Bytes on 64-bit platforms.
re.sub
Great news for regular expression lovers like myself! Regex-based substitutions will be faster by 2~3x for expressions containing group references (like \1
).
perf
The perf
profilers will now report function names in the trace logs.