Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Local/global variable behaviour of lists

(This might be, in some way, related to this bug).

I recently discovered the following behaviour, which I found quite surprising - if this is intentional, can someone please explain to me why this is supposed to happen?

def bla(x):
    x = 2

def bla2(L):
    L[0] = 2

def bla3(L):
    T = L
    T[0] = 2

Defining

x = 1
L = [1]

and then running bla(x) does not change the value of x, but running bla2(L) or bla3(L) DOES change the value of L to [2].

Local/global variable behaviour of lists

(This might be, in some way, related to this bug).

I recently discovered the following behaviour, which I found quite surprising - if this is intentional, can someone please explain to me why this is supposed to happen?

def bla(x):
    x = 2

def bla2(L):
bla2(K):
 L[0] K[0] = 2

def bla3(L):
bla3(K):
    T = L
K
    T[0] = 2

Defining

x = 1
L = [1]

and then running bla(x) does not change the value of x, but running bla2(L) or bla3(L) DOES change the value of L to [2].