https://github.com/buckley-w-david/deferpy
Go-like function deferral
https://github.com/buckley-w-david/deferpy
Last synced: 7 months ago
JSON representation
Go-like function deferral
- Host: GitHub
- URL: https://github.com/buckley-w-david/deferpy
- Owner: buckley-w-david
- License: mit
- Created: 2020-10-16T03:37:58.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-16T23:48:19.000Z (almost 5 years ago)
- Last Synced: 2025-01-28T02:12:24.535Z (9 months ago)
- Language: Python
- Size: 5.86 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# deferpy
```
Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.
```[The Go Blog](https://blog.golang.org/defer-panic-and-recover) has a good explanation of the behavior of `defer`. This package attempts to recreate the behavior as closely as possible.
## Installation
```bash
$ pip install deferpy
```## Usage
Check out the `tests` to see more examples.
```python
>>> from deferpy import defer
>>> @defer()
... def function(a, b, c):
... function.defer(print, a)
... function.defer(print, b)
... function.defer(print, c)
... return a + b + c
...
>>> print(function(1, 2, 3))
3
2
1
6
>>> @defer()
... def func():
... for i in range(10):
... func.defer(print, i)
...
>>> func()
9
8
7
6
5
4
3
2
1
0
```