Nov-27-2017, 09:31 PM
I'm trying to add each person to a list or dictionary. In this case it's a dictionary, but the same thing happens whether I reformat it to be a list that gets appended. I'm struggling to describe what is happening; other than I have a class with a list that whether I make it a instance member (self.lineage) or a class member as a static variable; the class only takes on the data of the instant. I thought perhaps that the methods variable 'l' was overriding the class method as it was originally called self.lineage.
When I add a for loop to show each iteration of what is going on it counts
0, 3, 3 with Person 2 and Person 3 retaining the values that Person 1 set. I'm not sure what's happening or what this is called, or why it's happening.
My goal is to add a person to a list, and then count the items in the list. I removed the original loop that did the counting since I realized my problem seems to be not understanding the anatomy of a class in python and how creating instances and working with variables and what gets overrided or not works.abs
Any help would be appreciated.
When I add a for loop to show each iteration of what is going on it counts
0, 3, 3 with Person 2 and Person 3 retaining the values that Person 1 set. I'm not sure what's happening or what this is called, or why it's happening.
My goal is to add a person to a list, and then count the items in the list. I removed the original loop that did the counting since I realized my problem seems to be not understanding the anatomy of a class in python and how creating instances and working with variables and what gets overrided or not works.abs
Any help would be appreciated.
class Person(): lineage = {} def __init__(self, data): self.data = data self.pid = 0 # self.lineage = [] def add_person(self, name): # renamed self.lineage to l to avoid instance member overriding class member l = Person(data).lineage self.name = name data["Name"] = self.name l.update(data) # prints the last instance # l = data # creates an empty list/dict print("Each single instance of Lineage") print(l) data = { "Name": "Unknown", "PID": 00000, "Attributes": { "Gender": "Unknown", "Sex": "Unknown", "DOB": "Unknown" } } # I've tried making different instances ex. a = Person(data), z.add_person, but # it doesn't seem to solve the issue and need a single variable command anyway. v = Person(data) y = v.add_person("Person1") y = v.add_person("Person2") y = v.add_person("Person3") print("Printing Total Lineage") y = v.lineage print(y) """ >>OUTPUT Each single instance of Lineage {'Name': 'Person1', 'PID': 0, 'Attributes': {'Gender': 'Unknown', 'Sex': 'Unknown', 'DOB': 'Unknown'}} Each single instance of Lineage {'Name': 'Person2', 'PID': 0, 'Attributes': {'Gender': 'Unknown', 'Sex': 'Unknown', 'DOB': 'Unknown'}} Each single instance of Lineage {'Name': 'Person3', 'PID': 0, 'Attributes': {'Gender': 'Unknown', 'Sex': 'Unknown', 'DOB': 'Unknown'}} Printing Total Lineage {'Name': 'Person3', 'PID': 0, 'Attributes': {'Gender': 'Unknown', 'Sex': 'Unknown', 'DOB': 'Unknown'}} """