__getitem__
class History:
def __init__(self, file_path:str, max_count:int=10):
self.file_path = file_path
self.max_count = max_count
self.data = []
def __getitem__(self, target):
if isinstance(target, int) and target <= self.max_count:
return self.data[target]
# 此时可以通过 xxxx[int] 的方式获取到属性
if ( __name__ == "__main__"):
tar = '.histroy'
histroy = History(tar)
histroy.add('ccvb')
histroy.add('1')
print(histroy[0]) # 1