python二次加工标准类型(包装)

版权声明:17602128911 https://blog.csdn.net/bus_lupe/article/details/85983374
# List继承原生列表list
class List(list):
    def append(self, p):
        if type(p) == str:
            # 用父类添加
            # list.append(self, p)
            super().append(p)
        else:
            print('必须是字符串')

    def show_mid(self):
        l = int(len(self) / 2)
        print(self[l])

arr = List('hello')
print(arr)
arr.show_mid()
arr.append('911')
arr.append(110)
print(arr)
'''
['h', 'e', 'l', 'l', 'o']
l
必须是字符串
['h', 'e', 'l', 'l', 'o', '911']
'''

猜你喜欢

转载自blog.csdn.net/bus_lupe/article/details/85983374
今日推荐