Last active
October 13, 2020 12:23
-
-
Save hugefiver/2dff5a648ca27962a5d8f7c55ef3c285 to your computer and use it in GitHub Desktop.
随便写写
This file contains hidden or 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
import re | |
p = re.compile(r'(get|set)(_[_a-zA-Z0-9]+)') | |
class A: | |
_x = 0 | |
def __getattr__(self, attr): | |
m = p.match(attr) | |
if m: | |
return (lambda: getattr(self, m.group(2))) \ | |
if m.group(1) == 'get' \ | |
else (lambda d: self.__setattr__(m.group(2), d)) | |
return self.__getattribute__(attr) | |
a = A() | |
print(a.get_x()) | |
a.set_x(10) | |
print(a.get_x()) | |
# 小明哥哥给的测试: | |
a.set_get_get_get_get_get_set_get_set_set_get_y('小明哥哥') | |
print('我找到你了,', a.get_get_get_get_get_get_set_get_set_set_get_y()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment