Skip to main content

python-defer

Golang-style defer with no decorators!

Defer is an awesome control flow construct. Python implementations have historically relied on cumbersome decorators or other manual coordination. No more!

Use python-defer to schedule code to be run at the end of a function, whether or not anything fails. Think of it like an automagic finally block that you can use inline anywhere.

Check out the blog post to learn how this was built.

Install

$ pip install python-defer

Usage

The default way to use python-defer is true magic. Simply write your code and append an in defer to it.

from defer import defer

def foo():
  print(", world!") in defer
  print("Hello", end="")
  # do something that might fail...
  assert 1 + 1 == 3
$ python foo.py
Hello, World!
Traceback (most recent call last):
  File "foo.py", line 7, in <module>
    assert 1 + 1 == 3
AssertionError

Sugarfree for me!

If you prefer a more explicit approach, you can use the d function, which takes a lambda.

from defer.sugarfree import defer as d

def foo():
  d(print, "1")
  print("2")
  d(lambda: print("3"))
  raise RuntimeError("oh no!")
  print("4")
$ python foo.py
2
3
1
Traceback (most recent call last):
  File "foo.py", line 7, in <module>
RuntimeError: oh no!

Metadata

Release files for python-defer 0.2.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 python-defer 0.2.1
File Size Uploaded Provenance
python_defer-0.2.1.tar.gz 3.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for python-defer 0.2.1
File Interpreter ABI Platform
python_defer-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / python_defer-0.2.1.tar.gz

Download URL python_defer-0.2.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6322328194054f6eb21372dcf0f7ac0b43c5e65218735aacc779fa4fbee27024
BLAKE2b-256 checksum
How to use checksums
9a110f00a8d598eccef9ef0435cf1deafc4592a552ba4dc73ea8a72a00ab8b79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.0 Darwin/22.1.0

Release files / python_defer-0.2.1-py3-none-any.whl

Download URL python_defer-0.2.1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df3736272a5c5349ce577f2d366508e4cd7e1eb0cbaf52a472ee1e1778d53ff9
BLAKE2b-256 checksum
How to use checksums
2b53a6a9ff260449aa8c7d7eedc07965ec833471835bff0455e8076a27e40bc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.0 Darwin/22.1.0

Security for python-defer 0.2.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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