Python Forum
Creating a list of class objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a list of class objects
#1
Hello all,

I'm trying to create a list of different objects using a loop.
I expect to get a list of different objects. But, in fact, I get a list of same objects.

path = "C:/SomethingRandom/file.txt"
myList = []
with open(path, 'r', 8192, 'utf8') as input_file:
   while True:
       line = input_file.readline()
       if len(line) != 0:
           classObject = className()
           classObject.getData(line)
           myList.append(classObject)
          del classObject
       else:
           print("End of file!")
           break
So... As I've checked using printf(), in every loop iteration string "line" changes it's value. But myList containts as many copies of first object (created using first "line"), as many lines my .txt file has. I have no idea why it happens.
Reply
#2
If I make a dummy class that just stores one instance attribute when getData is called, it works fine. I would expect the problem is in your classObject definition. Could be assigning to a class attribute by mistake?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-22-2017, 01:41 PM)ichabod801 Wrote: If I make a dummy class that just stores one instance attribute when getData is called, it works fine. I would expect the problem is in your classObject definition. Could be assigning to a class attribute by mistake?

The problem was in getData function. I used 3 lists in it, and I filled each of them using list.append(sth). No idea why, but when I declared these lists again in the function itself, everything started working perfectly.

Thanks a lot for answering!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 422 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  How can I access objects or widgets from one class in another class? Konstantin23 3 929 Aug-05-2023, 08:13 PM
Last Post: Konstantin23
  Creating list of lists, with objects from lists sgrinderud 7 1,561 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,499 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  How to store the resulting Doc objects into a list named A xinyulon 1 1,852 Mar-08-2022, 11:49 PM
Last Post: bowlofred
  Grouping and sum of a list of objects Otbredbaron 1 3,129 Oct-23-2021, 01:42 PM
Last Post: Gribouillis
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,883 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  apendng to a list within a class gr3yali3n 4 2,291 Feb-16-2021, 06:30 AM
Last Post: buran
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,273 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  Creating list of lists from generator object t4keheart 1 2,161 Nov-13-2020, 04:59 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020