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

  • SEARCH
  • Home
  • 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 6358019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:17:11+00:00 2026-05-24T23:17:11+00:00

I’m brand new to python, but I have a lot of experience with C++,

  • 0

I’m brand new to python, but I have a lot of experience with C++, Java, and .NET. I’m hoping that someone can explain why this sample program works the way it does and how I can change it to work the way I would like it to work.

Essentially, I’m trying to create a list of objects, and each object in the list contains a list of objects. In the end, the sub lists are all the same list, instead of being unique to the instance of their parent class.

class Obj1(object):
    list_of_obj2 = []
    name = ''

class Obj2(object):
    name = ''

list_of_obj1 = []

for i in range(2):
    new_obj1 = Obj1()
    new_obj1.name = "obj1_%s" % i
    print "Creating Object1 named %s" % new_obj1.name

    for j in range(2):
        new_obj2 = Obj2()
        new_obj2.name = "...obj2_%s_%s" % (i, j)

        new_obj1.list_of_obj2.append(new_obj2)
        print "...Added Object2 named %s to %s list" % (new_obj2.name, new_obj1.name)

    list_of_obj1.append(new_obj1)
    print "Added Object1 named %s to master list" % (new_obj1.name)

print ""

for obj in list_of_obj1:
    print "%s has %s items in list" % (obj.name, len(obj.list_of_obj2))
    for obj2 in obj.list_of_obj2:
        print "...%s" % obj2.name

The output of the program:

Creating Object1 named obj1_0
...Added Object2 named ...obj2_0_0 to obj1_0 list
...Added Object2 named ...obj2_0_1 to obj1_0 list
Added Object1 named obj1_0 to master list
Creating Object1 named obj1_1
...Added Object2 named ...obj2_1_0 to obj1_1 list
...Added Object2 named ...obj2_1_1 to obj1_1 list
Added Object1 named obj1_1 to master list

obj1_0 has 4 items in list
......obj2_0_0
......obj2_0_1
......obj2_1_0
......obj2_1_1
obj1_1 has 4 items in list
......obj2_0_0
......obj2_0_1
......obj2_1_0
......obj2_1_1

Why don’t the two Obj1 lists each have 2 items? Is there a better way to implement this?

  • 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-05-24T23:17:13+00:00Added an answer on May 24, 2026 at 11:17 pm

    list_of_obj2 is a class attribute of Obj1, if you want instance attribute:

    class Obj1(object):
        def __init__(self):
            self.list_of_obj2 = []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.