Can a class method change a variable in another class as an unforseen side effect?
<p>I'm having some problem with two classes. This is what I write in my main loop</p>

<pre><code>print(player1.getPosSize())    
ball.setPos(windowWidth, [player1.getPosSize(), player2.getPosSize()],
    [player1.getSpeed(), player2.getSpeed()])    
print(player1.getPosSize())
</code></pre>

<p>Here are the method definitions if it would help</p>

<p>(Ball class)</p>

<pre><code>def setPos(self, windowWidth, playerPosSizes, playerSpeed):       
    playerPosSizes[0].append(playerSpeed[0])
    playerPosSizes[1].append(playerSpeed[1])
    playerPosSizeSp = playerPosSizes
</code></pre>

<p>(Player Class)</p>

<pre><code>def getPosSize(self):
    return self.posSize

def getSpeed(self):
    return self.ysp
</code></pre>

<p>this is the output:</p>

<pre><code>[80, 285.0, 40, 150]    
[80, 285.0, 40, 150, 0.0]
</code></pre>

<p>So the list that <code>getPosSize()</code> returns is changed. This is weird because <code>getPosSize</code> returns a list <code>posSize</code> that exists ONLY in the class player. And I'm using the VALUE of <code>posSize</code> in the method of AN OTHER class. I don't see how <code>posSize</code> list can be changed! I mean when I call <code>getPosSize</code> I will get a copy of the <code>posSize</code>, right? So when I use that copy in the method <code>setPos</code> of the ball class the original <code>posSize</code> shouldn't change. </p>

<p>I'm really sorry if the code looks confusing I've tried to only include the relevant parts. </p>

<p>It looks like two variables in your code link to the same list, so changing one of them will change the other.</p>

<p>Didn't you do anything like <code>ball.posSize = player1.posSize</code>?</p>

<p>If yes, you created a second reference to the same list.</p>

<p>To fix this, change <code>ball.posSize = player1.posSize</code> to <code>ball.posSize = player1.posSize[:]</code></p>

python class variables methods