>>> class StrictList(list):
... def __init__(self,content=None):
... if not content:
... self.content = []
... self.type = None
... else:
... content = list(content)
... cc = content[0].__class__
... if l_any(lambda x: x.__class__ != cc, content):
... raise Exception("List items must be of the same type")
... else:
... self.content = content
... self.type = cc
...
>>> x = StrictList([1,2,3,4,5])
>>> x
[]
>>> x.content
[1, 2, 3, 4, 5]
我希望能够在调用x
而不是x.content
时返回内容
您正在尝试子类
list
,但从未调用list__init__
方法。添加以下内容:将项目添加到self。无需分配给
self.content
:相关问题 更多 >
编程相关推荐