Last active
September 22, 2015 02:25
-
-
Save alexrutherford/0258170b9859c5a88e0a to your computer and use it in GitHub Desktop.
Python function decorator to check types
This file contains 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
def check_args(*types): | |
def real_decorator(func): | |
def wrapper(*args, **kwargs): | |
for val, typ in zip(args, types): | |
assert isinstance(val, typ), "Value {} is not of expected type {}".format(val, typ) | |
return func(*args, **kwargs) | |
return wrapper | |
return real_decorator | |
def do_long_computation(name): | |
""" dummy function """ | |
time.sleep(10) return "FruitMart" | |
@check_args(str, int, int) | |
def print_fruit(name, apples, oranges): | |
store_name = do_long_computation(name) fruit = apples + oranges |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from this (Python guide)[https://www.airpair.com/python/posts/top-mistakes-python-big-data-analytics]