Last active
August 31, 2022 12:58
-
-
Save zmaplex/e05d7e6ac2a91a8f3f0b2317dac38f7f to your computer and use it in GitHub Desktop.
test.py
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
class CustomList(list): | |
def __getattribute__(self, item): | |
methods = ['append', 'clear', 'copy', 'extend', 'insert', 'pop', 'remove'] | |
if item not in methods: | |
return super(CustomList, self).__getattribute__(item) | |
method = super(CustomList, self).__getattribute__(item) | |
def length_change(*args, **kwargs): | |
data = method(*args, **kwargs) | |
# 改造下面一行代码就能实现通知了 | |
print(f"len:{len(self)}") | |
return data | |
return length_change | |
if __name__ == '__main__': | |
a = CustomList() | |
a.append(1) | |
a.append(1) | |
a.append(1) | |
a.append(1) | |
a.pop() | |
""" | |
len:1 | |
len:2 | |
len:3 | |
len:4 | |
len:3 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment