Skip to main content

Fancy defer for Python >= 3.12 (partially supported in Python 3.11)

Python package Coverage Status

Installation

You may install deferrer by running

# PyPI
pip install deferrer

or

# conda-forge
conda install -c conda-forge deferrer

.

Usage

defer

There are two designed ways to use defer. You may use either of them, or mix them up.

One is to use defer as a syntactic sugar in the form of defer and .... Example:

>>> from deferrer import defer

>>> def f():
...     defer and print("deferred")
...     print("normal")

>>> import sys
>>> if sys.version_info < (3, 12):
...     from deferrer import defer_scope
...     f = defer_scope(f)

>>> f()
normal
deferred

The other is to use defer as a function wrapper. Example:

>>> from deferrer import defer

>>> def f():
...     defer(print)("deferred")
...     print("normal")

>>> import sys
>>> if sys.version_info < (3, 12):
...     from deferrer import defer_scope
...     f = defer_scope(f)

>>> f()
normal
deferred

Note that when the deferred function can be invoke with no arguments, an empty call is not required, which means defer can be used as a decorator. Example:

>>> from deferrer import defer

>>> def f():
...     @defer
...     def _():
...         print("deferred")
...
...     print("normal")

>>> import sys
>>> if sys.version_info < (3, 12):
...     from deferrer import defer_scope
...     f = defer_scope(f)

>>> f()
normal
deferred

defer_scope

You can use defer_scope to declare a code range that deferred actions should be gathered in and executed after.

It's not rare that you may want to defer some actions in a loop and get them executed at the end of each cycle. But unlike some other languages, loops in Python don't create new scopes, which makes it inconvenient to use defer in them. defer_scope can be used to wrap an iterable to gather deferred actions in each iteration and execute them at the end of the iteration. Example:

>>> from deferrer import defer, defer_scope

>>> def f():
...     for i in defer_scope(range(3)):
...         defer and print("deferred", i)
...         print("normal", i)

>>> f()
normal 0
deferred 0
normal 1
deferred 1
normal 2
deferred 2

Sometimes, you may want to use defer outside of a function. defer_scope can be used as a context manager to gather deferred actions when it's entered and execute them when it's exited. Example:

>>> from deferrer import defer, defer_scope

>>> with defer_scope():
...     # Note that `defer and ...` itself is an expression, and
...     # its value (i.e. the `defer` object) may get printed by
...     # some interpretters.
...     # Use an assignment to suppress the printing behavior.
...     _ = defer and print("deferred")
...     print("normal")
normal
deferred

Also, defer_scope can be used to wrap a function to help defer to work properly in Python 3.11. Note that locals() in Python 3.11 returns a copy of the local scope, which makes it impossible for defer to inject deferred actions into the real local scope. Without a function level defer_scope, deferred actions would be executed immediately after they are evaluated.

Known Limitations

  • deferrer has only been tested on CPython. It may not work on other Python implementations.
  • defer must not be used together with await. Code like defer and await ... is syntactically acceptable but will cause segmentetation fault without being detected beforehand.

Metadata

Release files for deferrer 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for deferrer 1.1.1
File Size Uploaded
deferrer-1.1.1.tar.gz 14.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for deferrer 1.1.1
File Interpreter ABI Platform
deferrer-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.2 kB

Release files / deferrer-1.1.1.tar.gz

Download URL deferrer-1.1.1.tar.gz
Size 14.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a5cddd77724f79f271bd22d47466274093a0ec8c0b440424d2d749c36ad323b2
BLAKE2b-256 checksum
How to use checksums
d60d44670b060d1a16d5de63a9708da21b4c0fbbf1fadec58c2dfb33ae5f154b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 14, 2026.

Transparency log

Release files / deferrer-1.1.1-py3-none-any.whl

Download URL deferrer-1.1.1-py3-none-any.whl
Size 14.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3d0bac048c9ccee6e3e9b82bc18dec6337d004a9e32cbdd0c4fb68b23d1d5d0a
BLAKE2b-256 checksum
How to use checksums
846f9bb653c696c1fde839df00ce37d6b853652efc82b7945b1e5534c66e1387
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page