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 8048511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:27:48+00:00 2026-06-05T06:27:48+00:00

I have an issue in Python I couldn’t believe to find out. See the

  • 0

I have an issue in Python I couldn’t believe to find out. See the following code:

class Container(object):
    array = []

    def __init__(self):
        print self.array

for i in range(0, 5):
    container = Container()
    container.array.append('Test')
print 'Last Container:', container.array

The output is:

[]
['Test']
['Test', 'Test']
['Test', 'Test', 'Test']
['Test', 'Test', 'Test', 'Test']
Last Container: ['Test', 'Test', 'Test', 'Test', 'Test']

I thought the Container class is initialized with the values at the top on instantiation. Why is this not the case?

Thank you!

  • 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-05T06:27:50+00:00Added an answer on June 5, 2026 at 6:27 am

    Any attributes that you place directly inside of the class definition are class attributes, so Container.array is shared among all instances of Container.

    If you want an instance attribute instead, set self.array = [] inside of __init__():

        def __init__(self):
            self.array = []
            print self.array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Python code I often find myself doing the following (using DB-API): yValues
i have an issue i could use some help with, i have python list
I have a memory issue with mongoengine (in python). Let's say I have a
I have an issue with php where code works on on computer but wont
I'm have an issue with running the built in Python server that comes with
I have renamed a python class that is part of a library. I am
I have a python function func(A) . A is a numpy.array and can be
UDATE3: found the issue. See the answer below. UPDATE2: It seems I might have
I have a bit of an issue here. I have a Python script which
(Django 1.x, Python 2.6.x) I have models to the tune of: class Animal(models.Model): pass

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.