[issue42379] Optional List Args Persist Across Objects
New submission from Rose Ridder : When creating several objects in one program, if there are lists passed into functions as optional arguments, those lists are persistent across all objects. -- components: ctypes files: PythonObjectListIssue.py messages: 381153 nosy: rosadenderon priority: normal severity: normal status: open title: Optional List Args Persist Across Objects type: behavior versions: Python 3.7 Added file: https://bugs.python.org/file49604/PythonObjectListIssue.py ___ Python tracker <https://bugs.python.org/issue42379> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue42379] Optional List Args Persist Across Objects
Rose Ridder added the comment: Including script explicitly: # -*- coding: utf-8 -*- class Obj: def __init__(self, num): self.num = num self.var = self.funct() def funct(self, array = []): print (array) array = [1,2,3] 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 (obj2.num, obj2.var) # prints: 2 [1, 2, 3, 1, 2, 3] if __name__ == "__main__": main() -- ___ Python tracker <https://bugs.python.org/issue42379> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue42379] Optional List Args Persist Across Objects
Rose Ridder added the comment: 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() -- ___ Python tracker <https://bugs.python.org/issue42379> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com