Skip to content

Instantly share code, notes, and snippets.

@vimiix
Created December 18, 2019 10:42
Show Gist options
  • Save vimiix/92303743edefb9a60d27632f32ac6ac0 to your computer and use it in GitHub Desktop.
Save vimiix/92303743edefb9a60d27632f32ac6ac0 to your computer and use it in GitHub Desktop.
使装饰器同时支持带参数和不带参数
def ban_attr(ban):
if callable(ban):
def _wrapper():
raise AttributeError("Cannot write from Greywood to Dale")
return _wrapper
else:
assert isinstance(ban, bool), TypeError("ban_attr: want bool, got %s" % type(ban))
def _wrapper(f):
@wraps(f)
def __wrapper(*args, **kwargs):
if ban:
raise AttributeError("Cannot write from Greywood to Dale")
else:
return f(*args, **kwargs)
return __wrapper
return _wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment