1

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]))

1 Answer 1

4

The following will create a list of Pair objects when you're passing in a list with non-Pair objects, otherwise it will simply assign the given list of Pair objects to 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]

The reason you got the errors is that despite your check on the contents of pairs, you are still assigning it to 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]

Lastly, you should not do the following in a constructor (assigning to self):

self = Sequence([Pair(pos, val) for pos, val in enumerate(pairs)])

You're trying to 'overwrite' or 'replace' the self object but that means you're constructing another Sequence object in the constructor of Sequence. That is unnecessary in this case because you can handle both cases in the same constructor. That, in turn, leads to much cleaner code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.