Author rosadenderon
Recipients rosadenderon
Date 2020-11-16.20:49:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605559742.95.0.764837249994.issue42379@roundup.psfhosted.org>
In-reply-to
Content
Sorry, incorrect code pasted in. The full comment and code is below:

When creating several objects in one program, if there are lists passed into functions as optional arguments, those lists are persistent across all objects.

# -*- coding: utf-8 -*-

class Obj:
    def __init__(self, num):
        self.num = num
        self.var = self.funct()
        
    def funct(self, array = []):
        array += [1,2,3] # issue also occurs with .append()
        return array
    
    
    
def main():
    obj1 = Obj(1)
    print (obj1.num, obj1.var) # prints: 1 [1, 2, 3]

    obj2 = Obj(2)
    
    print (obj1.num, obj1.var) # prints: 1 [1, 2, 3, 1, 2, 3]
    print (id(obj1), id(obj1.var)) # prints a unique address for obj1, but the address for the var attribute is the same as for obj2
    print (obj2.num, obj2.var) # prints: 2 [1, 2, 3, 1, 2, 3]
    print (id(obj2), id(obj2.var)) # prints a unique address for obj2, but the address for the var attribute is the same as for obj1
    
    
if __name__ == "__main__": 
    main()
History
Date User Action Args
2020-11-16 20:49:02rosadenderonsetrecipients: + rosadenderon
2020-11-16 20:49:02rosadenderonsetmessageid: <1605559742.95.0.764837249994.issue42379@roundup.psfhosted.org>
2020-11-16 20:49:02rosadenderonlinkissue42379 messages
2020-11-16 20:49:02rosadenderoncreate