I have two classes Pair and Sequence. A Pair object has two attributes, position and value. A Sequence object is a sequence of Pair objects.
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]))
I can create a Sequence object with this:
>>> 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 >]
How can I create a Sequence object giving only Pair values list, like the code below? In this case, Pair position must be a sequence from 0 to n – 1, where n is the length of Sequence.
>>> Sequence([2, 8, 1])
< Seq. 2 8 1 >
>>> Sequence.pairs
[< Position: 0, Value: 2 >,
< Position: 1, Value: 8 >,
< Position: 2, Value: 1 >]
I tried to use this version of Sequence, but it didn’t work. I got this error:
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]))
The following will create a list of
Pairobjects when you’re passing in a list with non-Pairobjects, otherwise it will simply assign the given list ofPairobjects toself.pairs:The reason you got the errors is that despite your check on the contents of
pairs, you are still assigning it toself.pairs:Lastly, you should not do the following in a constructor (assigning to
self):You’re trying to ‘overwrite’ or ‘replace’ the
selfobject but that means you’re constructing anotherSequenceobject in the constructor ofSequence. That is unnecessary in this case because you can handle both cases in the same constructor. That, in turn, leads to much cleaner code.