菜鸟IT的博客 >> Python
继承内建类,自定义列表
class MyLieBiao(list):
# 利用list内建类,有1个可变参数传入
def __init__(self,*KeBian):
print("实例化")
super(MyLieBiao,self).__init__(*KeBian)
def __getitem__(self, item):
return super().__getitem__(item)
# mlb=MyLieBiao(("张三","李四"))
# print(mlb[0])
mlb=MyLieBiao(range(1,21))
print(mlb)
print("删除第3项")
del mlb[2]
print(mlb)
print("开始获取第5项")
print(mlb[4])
——————
输出结果:
实例化
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
删除第3项
[1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
开始获取第5项
6
进程已结束,退出代码0
菜鸟IT博客[2022.01.03-00:18] 访问:322