Last active
November 28, 2017 16:50
-
-
Save tsavola/767ae43292a534dab361958e13abbc43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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