Rose Ridder <[email protected]> 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 <[email protected]>
<https://bugs.python.org/issue42379>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com