Skip to content

Instantly share code, notes, and snippets.

@alexrutherford
Last active September 22, 2015 02:25
Show Gist options
  • Save alexrutherford/0258170b9859c5a88e0a to your computer and use it in GitHub Desktop.
Save alexrutherford/0258170b9859c5a88e0a to your computer and use it in GitHub Desktop.
Python function decorator to check types
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
@alexrutherford
Copy link
Author

Taken from this (Python guide)[https://www.airpair.com/python/posts/top-mistakes-python-big-data-analytics]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment