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

The Archive Base Latest Questions

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

Possible Duplicate: Assign a value to class variable is assiging it for all instances

  • 0

Possible Duplicate:
Assign a value to class variable is assiging it for all instances of that object

I need a help. I am used to java development and new I am starting python. And I write a simple class which contain a list of string. and In the main function I create a list of this object and I would like to print the content of the list of object. But my function print all the string of all the object together.
Here is an example

class FastData:
        name=''
        lines=[]

        def __init__ (self, name):
                self.name=name

        def setName(self,name):
                self.name = name

        def addLine(self, line):
                self.lines.append(line)

        def getLines(self):
                return self.lines

        def getName(self):
                return self.name

        def printList(self):

                for myline in self.lines:
                        print myline

if __name__ == '__main__':
    listes = []

    newData = FastData("Single")
    newData.addLine("1")
    newData.addLine("2")

    listes.append(newData)



    newData1 = FastData("Double")
    newData1.addLine("111")
    newData1.addLine("222")
    listes.append(newData1)

    for myList in listes:
        print myList.getName()
        myList.printList()

and Here is what I got like output

Single
1
2
111
222

Double
1
2
111
222

Thank for your help

Thathug

  • 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-26T03:17:25+00:00Added an answer on May 26, 2026 at 3:17 am

    In your code, all instances of FastData share the same lines object. Fix it like so:

    class FastData:
    
        # note that I've removed `name' and `lines' from here
    
        def __init__ (self, name):
                self.name = name
                self.lines = []
    

    The reason your code worked the way it did was that lines was a property of the class, not of each individual instance of the class.

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

Sidebar

Related Questions

Possible Duplicate: need help with some stored procedure plz ERROR:- A SELECT statement that
Possible Duplicate: How do I assign by “reference” to a class field in c#?
Possible Duplicate: declare property as object? in java you can create an object directly
Possible Duplicate: Difference between class property mVar and instance variable self.mVar I am new
Possible Duplicate: How To Set Text In An EditText how can we assign value
Possible Duplicate: How to assign Profile values? I'm reading an ASP.NET book that says
Possible Duplicate: Idiomatic object creation in ruby Sometimes it's useful to assign numerous of
I want to duplicate a class. It is sufficient that I copy all properties
Possible Duplicate: Algorithm for generating a random number Hi i need to assign a
Possible Duplicate: How to assign the result of the previous expression to a variable

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.