Reputation: 13
Either the answer to that question will work, or I've read that you don't need to actually name them... so i tried this:
class Entry():
__slots__ = ('name', 'gender', 'occurances')
def mkEntry(name_foo, gender_foo, occurances_foo):
myEntry = Entry
myEntry.name = name_foo
myEntry.gender = gender_foo
myEntry.occurances = occurances_foo
return myEntry
def topTwenty(fileName):
file = open(fileName)
topTwenty = []
femaleCount = 0
maleCount = 0
for line in file:
a = line.split(",")
if a[1] == 'F' and femaleCount < 20:
topTwenty.append(mkEntry(a[0], a[1], a[2]))
femaleCount = femaleCount + 1
print(topTwenty[7].name)
but the print(topTwenty[7].name) is printing what i expect from topTwenty[20].name
any help?
Upvotes: 0
Views: 344
Reputation: 251578
You need to do myEntry = Entry()
to create an instance of the class. Right now you are just overwriting the attributes of the same class every time, and appending that same class to the list every time. Your list just contains the same item 20 times.
This is a weird way to do things anyway. Why don't you move that code from mkEntry
into an __init__
on the Entry
class, to keep the initialization together with the class.
Upvotes: 1