S
SuperZE
Learning python I was rewriting some of my old programs to see the
pros and cons of python when a steped in some weird (at least for me)
behavior.
Here it is simplified
The code:
myList = [4 for n in range(4)]
myInt = 4
I would not expect the second a.myList to have changed as it did
since, for me, I have only changed b.myList. And also, why only the
list changed and not the integer?
One thing i tried was:
myList = []
myInt = 4
def __init__(self):
self.myList = [4 for n in range(4)]
And as you see it worked as I expected.
Now the question, why?
pros and cons of python when a steped in some weird (at least for me)
behavior.
Here it is simplified
The code:
myList = [4 for n in range(4)]
myInt = 4
4a = Test1()
b = Test1()
a.myList [4, 4, 4, 4]
a.myInt 4
b.myList [4, 4, 4, 4]
b.myInt 4
b.myList[2] = 3
b.myInt = 3
b.myList [4, 4, 3, 4]
b.myInt 3
a.myList [4, 4, 3, 4]
a.myInt
I would not expect the second a.myList to have changed as it did
since, for me, I have only changed b.myList. And also, why only the
list changed and not the integer?
One thing i tried was:
myList = []
myInt = 4
def __init__(self):
self.myList = [4 for n in range(4)]
[4, 4, 4, 4]a = Test2()
b = Test2()
a.myList [4, 4, 4, 4]
b.myList [4, 4, 4, 4]
b.myList[2] = 3
b.myList [4, 4, 3, 4]
a.myList
And as you see it worked as I expected.
Now the question, why?