我有一个从列表继承的类。如何在创建实例时分配列表而不是在创建后附加到实例?示例代码:
class ListObject(list):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
premade_normal_list = [0, 1, 2, 3, 4, 5, 6]
_list = ListObject(1, 2, 3) # Can I explicitly assign the premade list as this
# object while retaining attributes?
# How I now have to do it.
premade_normal_list = [0, 1, 2, 3, 4, 5, 6]
_list = ListObject(1, 2, 3)
for i in premade_normal_list:
_list.append(i)
我试过了,这并不奇怪:
class ListObject(list):
def __init__(self, a, b, c, _list):
self = _list
self.a = a
self.b = b
self.c = c
premade_normal_list = [0, 1, 2, 3, 4, 5, 6]
_list = ListObject(1, 2, 3, premade_normal_list)
我很难解释,希望它足够清楚......
拉风的咖菲猫
明月笑刀无情
相关分类