Skip to content

Instantly share code, notes, and snippets.

@tsavola
Last active November 28, 2017 16:50
Show Gist options
  • Select an option

  • Save tsavola/767ae43292a534dab361958e13abbc43 to your computer and use it in GitHub Desktop.

Select an option

Save tsavola/767ae43292a534dab361958e13abbc43 to your computer and use it in GitHub Desktop.
import functools
class CheckBool:
def __init__(self, value):
assert isinstance(value, bool)
self._value = value
self._checked = False
def __del__(self):
assert self._checked
def __nonzero__(self):
self._checked = True
return self._value
def check_result(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
return CheckBool(f(*args, **kwargs))
return wrapper
def discard(x):
if isinstance(x, CheckBool):
x._checked = True
@check_result
def inspect_parameter(param: str) -> bool:
return param is not None
if __name__ == "__main__":
if inspect_parameter("this works"):
print("yeah")
discard(inspect_parameter("this works"))
inspect_parameter("this shouldn't work")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment