Skip to main content

Provides Go-like `defer`

Project description

Provides Go-like defer

Powered by Yamato Nagata


installation: $ pip install defern

from defern import defern, deferner, defern_this, here

def defer_multiple_function(frame):
    defern(lambda: print("it's middle 7th"), frame=frame)
    defern(lambda: print("it's middle 8th"), frame=frame)


defern(lambda: print("it's middle 1st"))  # you can pass function to run after return.
defern(lambda: print("it's middle 2nd"))
defern(print, "it's middle 3rd")  # passed args and kwargs after function will given to function.

def defer_this(number: str):
    print("it's middle", number)  # you can create function which runs after return with `@deferner`

defer_this("5th", frame=here())

@defern_this  # wrap function to create procedure which automatically runs after return
def defer_this_now():
    print("it's middle 6th")

defer_multiple_function(here())  # here() to get currentFrame and pass it to `defern` or function created with `deferner`
                                 # with name `frame` will create `defern` to that frame


# Hi
# ended
# it's middle 1st
# it's middle 2nd
# it's middle 3rd
# it's middle 4th
# it's middle 5th
# it's middle 6th
# it's middle 7th
# it's middle 8th

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

defern-1.0.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

defern-1.0.0-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page