1. 程式人生 > >python二次加工標準型別(包裝)

python二次加工標準型別(包裝)

# 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']
'''