-
-
Save no1xsyzy/e3cbd637bcfffef24004d5a44b8fd26d 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
# 重度采用词法作用域。这对 Python 不友好,尤其因为有内部指针循环引用,回收依赖新 GC。 | |
class Account: | |
def __init__(self, **kwargs): | |
self._is_valid = True | |
def valid_before_logout(func): | |
def wrapped(*args, **kwargs): | |
if self._is_valid: | |
return func(*args, **kwargs) | |
else: | |
raise ValueError('not valid') | |
return wrapped | |
@valid_before_logout | |
def refresh(): | |
pass | |
@valid_before_logout | |
def invalidate(): | |
self._is_valid = False | |
@valid_before_logout | |
def signout(): | |
self._is_valid = False | |
self.refresh = refresh | |
self.invalidate = invalidate | |
self.signout = signout |
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
# Pythonic | |
def valid_before_logout(func): | |
def wrapped(self, *args, **kwargs): | |
if self._is_valid: | |
return func(self, *args, **kwargs) | |
else: | |
raise ValueError('not valid') | |
return wrapped | |
class Account: | |
def __init__(self, **kwargs): | |
self._is_valid = True | |
@valid_before_logout | |
def refresh(self): | |
pass | |
@valid_before_logout | |
def invalidate(self): | |
self._is_valid = False | |
@valid_before_logout | |
def signout(self): | |
self._is_valid = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment