由于时间关系,这里以列表作为示例,对列表进行包装,生成自己的序列。
预计下一篇文章会对迭代器、生成器、range、file对象等进行详解
# 定义自己的序列
import datetime
import pandas as pd
class MySequence(object):
def __init__(self, sequence):
"""
如果传入的序列是字典,代码还需要再完善一下。这里先剔除字典。等后面有时间了再加进来。
:param sequence:
"""
# if not isinstance(sequence, (list, tuple, set, dict, pd.Series)):
if not isinstance(sequence, (list, tuple, set, pd.Series)):
raise TypeError("请输入正确的序列")
# self.__sequence = list(sequence) if isinstance(sequence, (list, tuple, set, pd.Series)) else sequence
self.__sequence = list(sequence)
self.__index = -1
self.start_time = datetime.datetime.now().strftime("%F %H:%M:%S")
def __str__(self):
"""
实例化时,print对象名,输出return内容
:return:
"""
return f"MySequence({self.__sequence})"
def __iter__(self):
"""
可迭代对象
:return:
"""
return self
def __next__(self):
"""
功能一:迭代器必须要有__iter__和__next__方法
功能二:实例化后,print(list(对象名)),输出一个列表
:return:
"""
self.__index += 1
if self.__index >= self.__sequence.__len__():
raise StopIteration
return self.__sequence[self.__index]
def __setitem__(self, key, value):
self.__sequence[key] = value
def __getitem__(self, item):
return self.__sequence[item]
def __delitem__(self, key):
del self.__sequence[key]
def __len__(self):
return self.__sequence.__len__()
def __sizeof__(self):
return self.__sequence.__sizeof__()
def __call__(self, *args, **kwargs):
return self.__sequence
def __del__(self):
# print("This is a self-defined-sequence demo %s" % datetime.datetime.now().strftime("%F %H:%M:%S")
# ) # 报错 ImportError: sys.meta_path is None, Python is likely shutting down
print("This is a self-defined-sequence demo %s" % self.start_time)
if __name__ == '__main__':
mr = MySequence([1, 2, 3, 4, 5, 6, 7])
# 需要注意:如果注释掉__str__,
# (1)print(mr)不会返回列表,返回的是<__main__.MySequence object at 0x000001B038789EF0>
# (2)print(mr[index])的切片操作可以正常输出
print(mr) # __str__发挥作用 MySequence([1, 2, 3, 4, 5, 6, 7])
print(mr()) # __call__发挥作用 [1, 2, 3, 4, 5, 6, 7]
print(list(mr)) # __next__发挥作用 [1, 2, 3, 4, 5, 6, 7]
del mr[1] # 删除操作,__delitem__发挥作用 [1, 3, 4, 5, 6, 7]
print(mr())
print(list(mr)) # 这里返回空列表,是因为迭代已停止
mr[1] = 'My Sequence' # 更改值操作,__setitem__发挥作用 [1, 'My Sequence', 4, 5, 6, 7]
print(mr())
print(mr[1]) # 切片操作,__getitem__发挥作用 My Sequence
print(mr[1:4]) # 切片操作,__getitem__发挥作用 ['My Sequence', 4, 5]
print(len(mr)) # __len__发挥作用(如果注释掉__len__会报错TypeError: object of type 'MySequence' has no len())
for i in mr(): # __next__和__iter__在起作用
print(i)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
PS:。。。。。。
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
运行结果截图
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!