Skip to content

Instantly share code, notes, and snippets.

@zmaplex
Last active August 31, 2022 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmaplex/e05d7e6ac2a91a8f3f0b2317dac38f7f to your computer and use it in GitHub Desktop.
Save zmaplex/e05d7e6ac2a91a8f3f0b2317dac38f7f to your computer and use it in GitHub Desktop.
test.py
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