Python list of object are sharing variables -


class journey has list, self.leg_miles

class manyjourneys has list of journeys created by

self.journeys = [] in range(2):     self.journeys.append(journey()) 

from input add list leg_miles each journey kind of this:

self.journeys[self.count].addleg(temp) 

temp number read list. changed inputted right before above line.

for reason instead of creating new list journey[1] adds created list.

for example: if journey[0] leg_miles had [4,5,6] , moved on journey[1] add 4 , 6 have : leg_miles = [4,5,6,4,6]

i not understand why adding on. have self in it. not understand how upload code or pictures.

i hope enough information solve problem

edit: here code have.

class journey:      def __init__(self,odometer=0,leg_miles=[],leg_gas=[]):  #constructor         self.odometer = odometer         self.leg_miles = leg_miles         self.leg_gas = leg_gas      def addleg(self,miles,gas):         #adds list of miles , gas          self.leg_miles.append(miles)         self.leg_gas.append(gas)      def getlists(self):         #returns list of miles , list of gas          return self.leg_miles,self.leg_gas      def calculate(self):         #calculate miles per litre journey , return         miles_per_litre = 0         in range(len(self.leg_miles)):    #calcs miles per litre each leg , adds total             miles_per_litre += int(self.leg_miles[i]/self.leg_gas[i])         return miles_per_litre   class manyjourneys:      def __init__(self,name):          self.myfile = open(name,"r")         self.content = self.myfile.read().split("\n")          self.journeys = []          in range(self.content.count("")+1):             self.journeys.append(journey())          self.count = 0          in self.content:             if == "":                 self.count+=1             else:                 temp = i.split(" ")                 self.journeys[self.count].addleg(int(temp[0]),int(temp[1]))        def statistics(self):          sum = 0         in self.journeys:             sum += i.calculate()          return sum/len(self.journeys)   def main():     filename  = input("please enter file name. (eg: test.txt): ")     manyjourneys1 = manyjourneys(filename)     print("you got {0} miles per litre.".format(manyjourneys1.statistics()))  main() 

and sample text file be

100 54 340 109 23 4  333 33 4500 678 

there unobvious feature in python regarding default values - represent same object each time, causes surprising behavior when using mutable values defaults:

def add(value, l=[]):     l.append(value)     return l  add(1) # [1] add(2) # [1, 2] 

to not fall trap not use list or other mutable value way. if need initialize empty list in function/method body:

def __init__(self, odometer=0, leg_miles=none, leg_gas=none):     self.odometer = odometer     self.leg_miles = leg_miles or []     self.leg_gas = leg_gas or [] 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -