GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - yasyf/python-defer: Golang-style `defer` with no decorators!

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
.vscode
.vscode
 
 
defer
defer
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
poetry.lock
poetry.lock
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

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!

About

Golang-style `defer` with no decorators!

Resources

Readme
Activity

Stars

26 stars

Watchers

3 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.