Copy by reference or value in Python


In most cases, if you are learning a new language, you will have to figure out in what cases, variables are copied by references and in what other cases, they are passed by values.

I am writing an Simulated Annealing class in Python and an example class of using it. The code can be downloaded in https://github.com/DoctorLai/PySA. Actually, I didn’t read thoroughly any python reference books/tutorials before starting writing something complex and interesting. I thought that variables are copied by values first, but it is not always the case.

For example, the Python treats all basic data types e.g. single character, float, number, similarly as other programming languages. These data types are copied by default (on assignment). 

a = 1
b = a
# a = 2, b = 1
a = 2
# output 2   1
print a, b

The above code snippet illustrates the case that integers are copied by value instead of reference. Both variables and are independent copies in memory. However, it is totally different regarding the list object in Python.

a = [1, 2, 3, 4]
b = a
a[0] = 0
# output [0, 2, 3, 4]
print b

The above shows that and lists are both pointing to the same memory locations. Changing one will also change the other.

How about parameter passing in functions? Let’s take a look.

n = 1

def test(x):
    x = 2

# before = 1
print 'before = ', n
test(n)
# after = 1
print 'after = ', n

Well, this is not diffcult to expect. Integer variables are passed by copies to functions and therefore their values are local to functions. This will be the opposite if you pass lists as parameters to functions.

n = [1, 2, 3, 4]

def test(x):
    x[0] = 5

# before = [1, 2, 3, 4]
print 'before = ', n
test(n)
# after = [5, 2, 3, 4]
print 'after = ', n

Well, the following is a special case. 

a = [1, 2, 3]
b = a
b.append(4)
b = ['a', 'b']
# prints [1, 2, 3, 4] ['a', 'b']
print a, b

This is because re-assigning will not change the origin list. If you want to change some variables inside a function that can reflect the changes outside. You may also use global keyword to directly change the variable. For example,

n = 0

def test():
    global n
    n = 1

# before = 0
print 'before = ', n
test()
# after = 1
print 'after = ', n

However, if you don’t want to change the global vars, or just simply want to get its values, you don’t need the global keyword:

x = 1

def test():
    print x # prints 1, ok without global

test()    

Without global keyword, the local copies of vars will be created. Changes won’t reflect to global vars.

x = 1
def test():
    x = 2  # creates local copy
test()
print x  # still '1'

If you want a separate copy of list, you can easily do this in Python:

a = [1, 2, 3, 4]
b = [x for x in a]
a[0] = 0
# output [0, 2, 3, 4] [1, 2, 3, 4]
print a, b

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
554 words
Last Post: Codeforces: B. Permutation
Next Post: Codeforces: A. Football

The Permanent URL is: Copy by reference or value in Python

Leave a Reply