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>Any <code>mutable</code> object such as <code>list</code> can be changed by anything that has access to it. Ie. if your <code>getPosSize()</code> method is returning the<code>list</code> itself, you can make changes that will affect the same list.</p>

<pre><code>class One(object):
    CLS_LST = [1, 2, 3]
    def __init__(self):
        self.lst = [4, 5, 6]

    def get_cls_pos(self):
        return One.CLS_LST

    def get_pos(self):
        return self.lst

class Two:
    def do_smthng(self, data):
        data.append(-1)



a = One()

print('This is One.CLS_LST: {}'.format(a.CLS_LST))
print('This is a.lst: {}'.format(a.lst))
b = Two()
print('Calling b do something with One.CLS_LST')
b.do_smthng(a.get_cls_pos())
print('This is One.CLS_LST: {}'.format(a.CLS_LST))
print('Calling b to do something with a.lst')
b.do_smthng(a.get_pos())
print('This is a.lst: {}'.format(a.lst))
</code></pre>

<p>The result of this code will print:</p>

<pre><code>This is One.CLS_LST: [1, 2, 3]
This is a.lst: [4, 5, 6]
Calling b to do something with One.CLS_LST
This is One.CLS_LST: [1, 2, 3, -1]
Calling b to do something with a.lst
This is a.lst: [4, 5, 6, -1]
</code></pre>

<p>If you really want to pass the copy of list, try to use <code>return list[:]</code>.</p>

python class variables methods