python - Appending modified object to list in for loop -- only last modification is stored -


i created function goes through loop, creates variables, , appends them in list.

    def function(self,record):         temp_list_records=[]         #depends number of x, got 10, 100,1000,10000 loops         loops=int(math.pow(10,x_range))         digits="%0"+str(x_range)+"d"          #then create records , add mysql data query         x in range (0,loops):              end_of_number=digits%x             record.dn=dn_number+str(end_of_number)             record.gn=gn_number+str(end_of_number)              temp_list_records.append(record)          return temp_list_records 

explanation: pass object called record has 2 properties, dn , gn. these properties have structure: 0000213123xx, xx two-digit number. need generate values xx in range between 00 , 99, properties be: 000021312300, 000021312301,... every time append list record, record dn , gn change in range. explain later, during loop, numbers found, once loop finished, objects in list become last 1 found.

then values returned in list are:

 000021312399 000021312399 000021312399 000021312399 000021312399 000021312399 000021312399 000021312399 

but, if put print inside loop:

def function(self,record):         temp_list_records=[]         #depends number of x, got 10, 100,1000,10000 loops         loops=int(math.pow(10,x_range))         digits="%0"+str(x_range)+"d"          #then create records , add mysql data query         count=0         x in range (0,loops):              end_of_number=digits%x             record.dn=dn_number+str(end_of_number)             record.gn=gn_number+str(end_of_number)              temp_list_records.append(record)             print temp_list_records[count].dn             count+=1          return temp_list_records 

the result of prints are:

 000021312300 000021312301 000021312302 000021312303 000021312304 000021312305 000021312306 000021312307 ... 

can explain me why? how solve this?

as @martineau has commented, problem you've appended temp_list_records bunch of references same object, record. hence, when loop finished, contents of list same.

this can fixed one-line change code. have re-initialize record inside each iteration of loop (so each loop has separate instance):

def function(self,record):     temp_list_records=[]     #depends number of x, got 10, 100,1000,10000 loops     loops=int(math.pow(10,x_range))     digits="%0"+str(x_range)+"d"      #then create records , add mysql data query     x in range (0,loops):          record = get_new_record()  # change.          end_of_number=digits%x         record.dn=dn_number+str(end_of_number)         record.gn=gn_number+str(end_of_number)          temp_list_records.append(record)      return temp_list_records 

of course, replace get_new_record call appropriate class.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -