Skip to content

Instantly share code, notes, and snippets.

@halit
Forked from fatiherikli/__init__.py
Created September 18, 2012 21:10
Show Gist options
  • Save halit/3745920 to your computer and use it in GitHub Desktop.
Save halit/3745920 to your computer and use it in GitHub Desktop.
decorator examples
def accepts(*accepted_types):
def decorated_function(function):
def inner(*passed_args):
for accepted_type, passed_arg in zip(accepted_types, passed_args):
if not isinstance(passed_arg, accepted_type):
raise TypeError
return function(*passed_args)
return inner
return decorated_function
def returns(*return_types, **kwargs):
allow_none = kwargs.get("allow_none", True)
if allow_none:
return_types = return_types + (type(None), )
def decorated_function(function):
def inner(*args):
result = function(*args)
if not isinstance(result, tuple(return_types)):
raise TypeError
return result
return inner
return decorated_function
@accepts(int, int)
@returns(int, allow_none=True)
def add(x, y):
return
print add(1,2)
#print accepts(int, int)(add)(3,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment