Created
December 18, 2019 10:42
-
-
Save vimiix/92303743edefb9a60d27632f32ac6ac0 to your computer and use it in GitHub Desktop.
使装饰器同时支持带参数和不带参数
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 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