Skip to content

L3viathan/gofuncyourself

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

gofuncyourself

Golang has this... interesting approach to exception handling where functions return a tuple of the actual result and a error (that may be nil).

This is a reimagining of the same concept applied to Python. It comes in form of a decorator:

from gofuncyourself import gofunc

@gofunc
def div(x, y):
    return x / y

If you now call div(16, 2) it will return (8, None), where None references the fact that no error has occurred.

If you instead do something like res, err = div(7, 0) then res will be None, and err will not; instead it will be a special error object that contains the exception that was caught in err.exception.

Functions decorated with gofunc are to be called like this:

res, err = div(x, y)
if err:
    ...  # handle exception

But wait, there's more!


What if you forget to check for an error? gofuncyourself protects you from that, by raising the exception one second later, if you didn't do if err:. The delay can be adjusted by passing a delay keyword argument to the decorator. For example, if you want to make sure the error is handled swiftly:

@gofunc(delay=0.1)  # only wait for 100ms before raising
def div(x, y):
    return x / y

Installation

pip install gofuncyourself

Acknowledgements

About

Go-style errors in Python, with a twist

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages