Skip to content

Instantly share code, notes, and snippets.

@AeroNotix
Created March 22, 2012 10:46
Show Gist options
  • Select an option

  • Save AeroNotix/2157646 to your computer and use it in GitHub Desktop.

Select an option

Save AeroNotix/2157646 to your computer and use it in GitHub Desktop.
def arg_types(*types):
def wrapper(function):
def inner_wrap(*wrapped_args):
if len(types) != len(wrapped_args):
raise Exception(''.join(
[
'Incorrect argument amount, this ',
'argument requires {} argument and ',
'you provided {} to the decorator.']
).format(len(wrapped_args), len(types)))
for pair in zip(types, wrapped_args):
if type(pair[1]) != pair[0]:
raise TypeError
return function(*wrapped_args)
return inner_wrap
return wrapper
@arg_types(int)
def type_hinted_function(x, y):
return x, y
print type_hinted_function(1, 10)
type_hinted_function('1', 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment