Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Appending list with class-object overwrites all previous list elements

Mareike:

I spent quite some time trying to figure out what the problem is and already tried several ways that I found in other questions but still it does not work. I want to store all my class objects in the list "houselist" but instead of appending the list it just gives me several time the same entry. I guess it's some kind of reference issue but I can't figure it out. Here's the code:


# Definition of the house object class
class House_object():
def __init__(self, price, surfaceArea, rent, tNumber, adress, monthlyCost, areaCost):
self.price = price
self.surfaceArea = surfaceArea
self.rent = rent
self.tNumber = tNumber
self.adress = adress
self.monthlyCost = monthlyCost
self.areaCost = areaCost

#reading the file
f = open("bfil.txt", "r")
content = f.read()
splitlist = content.split("\n")
f.close()

num_lines = sum(1 for line in open('bfil.txt'))

#creating the list "houselist" and adding the class instances to it
houselist = []
for i in range(0, num_lines, 5):
price = int(splitlist[0 + i])
surfaceArea = int(splitlist[1 + i])
rent = int(splitlist[2 + i])
tNumber = splitlist[3 + i]
adress = splitlist[4 + i]
monthlyCost = int(rent + (((price - dPayment) * (bankRate / 100) * (1 - (intrestDeduct / 100))) / 12))
areaCost = int(price / rent)
bostad = House_object(price, surfaceArea, rent, tNumber, adress, monthlyCost, areaCost)

houselist.append(bostad) # I think here might be the problem??

for element in houselist:
print("this is the list during the loop: " + bostad.adress)

The print command at the end gives me the following:


this is the list during the loop: Streetgatan 1
this is the list during the loop: KTHstreet 7
this is the list during the loop: KTHstreet 7
this is the list during the loop: weststreet 66
this is the list during the loop: weststreet 66
this is the list during the loop: weststreet 66
this is the list during the loop: Delilstreet 57
this is the list during the loop: Delilstreet 57
this is the list during the loop: Delilstreet 57
this is the list during the loop: Delilstreet 57

So with every Loop, python overwrites my list and adds the new instance but replaces also the already existing ones. I would have expected this:


this is the list during the loop: Streetgatan 1
this is the list during the loop: Streetgatan 1
this is the list during the loop: KTHstreet 7
this is the list during the loop: Streetgatan 1
this is the list during the loop: KTHstreet 7
this is the list during the loop: weststreet 66
this is the list during the loop: Streetgatan 1
this is the list during the loop: KTHstreet 7
this is the list during the loop: weststreet 66
this is the list during the loop: Delilstreet 57

I tried both houselist.append(bostad) and houselist = houselist[:] + bostad

I read in some other posts about this possibility, so I tried this, but still gives me the same result. I am very thankful for any help!



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Appending list with class-object overwrites all previous list elements

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×