Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:20:23+00:00 2026-06-06T21:20:23+00:00

I have two classes Pair and Sequence . A Pair object has two attributes,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:20:24+00:00Added an answer on June 6, 2026 at 9:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, class A and class B. Class A has a mission
I have the following two classes: class Key<T1, T2> {} class Pair<F, S> {}
I have two classes: class Translation(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() object =
I have two classes: public class CourseModule { // attributes... List<Course> courses; public void
I have two classes defined in different h files and each class has some
I have two classes that extend the activity class. Each class has it's own
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes Foo and Bar that Bar extends Foo as below: class
I have two classes, one that inherits from the other. The base class is
I have two classes set up as follows: class Point { protected: double coords[3];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.