我有两个班级Pair
和Sequence
。 Pair
对象有两个属性,position和value。 Sequence
对象是一系列Pair
个对象。
class Pair():
def __init__(self, position, value):
self.position = position
self.value = value
self.pair = (position, value)
def __repr__(self):
return "< Position: {0}, Value: {1} >".format(self.position, self.value)
class Sequence(Pair):
def __init__(self, pairs):
self.pairs = pairs
self.cseg = [pair.value for pair in pairs]
self.positions = [pair.position for pair in pairs]
def __repr__(self):
return "< Seq. {0} >".format(" ".join([str(x) for x in self.cseg]))
我可以用这个创建一个Sequence
对象:
>>> Sequence([Pair(0, 2), Pair(2, 8), Pair(3, 1))
< Seq. 2 8 1 >
>>> Sequence.pairs
[< Position: 0, Value: 2 >,
< Position: 2, Value: 8 >,
< Position: 3, Value: 1 >]
如何创建仅提供Sequence
值列表的Pair
对象,如下面的代码?在这种情况下,Pair
位置必须是从0到n - 1的序列,其中n是Sequence
的长度。
>>> Sequence([2, 8, 1])
< Seq. 2 8 1 >
>>> Sequence.pairs
[< Position: 0, Value: 2 >,
< Position: 1, Value: 8 >,
< Position: 2, Value: 1 >]
我尝试使用此版本的Sequence
,但它不起作用。我收到了这个错误:
AttributeError: 'int' object has no attribute 'value'
class Sequence(Pair):
def __init__(self, pairs):
if not isinstance(pairs[0], Pair):
self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])
self.pairs = pairs
self.cseg = [pair.value for pair in pairs]
self.positions = [pair.position for pair in pairs]
def __repr__(self):
return "< Seq. {0} >".format(" ".join([str(x) for x in self.cseg]))
答案 0 :(得分:4)
当您传入包含非Pair
对象的列表时,以下内容将创建Pair
个对象的列表,否则它只会将给定的Pair
个对象列表分配给self.pairs
:
class Sequence(Pair):
def __init__(self, pairs):
if not isinstance(pairs[0], Pair):
self.pairs = [Pair(pos, val) for pos, val in enumerate(pairs)]
else:
self.pairs = pairs
self.cseg = [pair.value for pair in self.pairs]
self.positions = [pair.position for pair in self.pairs]
您收到错误的原因是,尽管您检查了pairs
的内容,但仍然将其分配给self.pairs
:
class Sequence(Pair):
def __init__(self, pairs):
# the following code will run when you're passing a list of integers
if not isinstance(pairs[0], Pair):
self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])
# but the next line will also run
# which means self.pairs` is now a list of integers
self.pairs = pairs
# and that means pair in the following line is an int, not a Pair:
self.cseg = [pair.value for pair in pairs]
self.positions = [pair.position for pair in pairs]
最后,您不应在构造函数中执行以下操作(分配给self
):
self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])
您尝试“覆盖”或“替换”self
对象,但这意味着您正在Sequence
的构造函数中构建另一个Sequence
对象。在这种情况下,这是不必要的,因为您可以在同一个构造函数中处理这两种情况。反过来,这会导致更清晰的代码。