2

I'm a complete beginner to python (from C/C++) I'm just practicing on python from few days and when I observed the behavior of the below lines, I'm confused.

#Integer case
num1 = 1
num2 = num1
print num1, num2
num1 += 1
print num1, num2

#List case
list1 = [1,2,3,4,5]
list2 = list1
print list1,list2
list1[2:2] = [0,0,0]
print list1, list2

This piece of code gave me the following result:

1 1

2 1

[1,2,3,4,5] [1,2,3,4,5]

[1,2,0,0,0,3,4,5] [1,2,0,0,0,3,4,5]

Here,the change of value of num1 is not reflected in num2. But the change in list1 is reflected in list2. I'm confused with this behavior, please clarify (I'm a beginner, explanation from a beginner point of view is greatly appreciated)

edited:

and how can I achieve a copy of list1 (without actually creating a reference)?

Take a case where I have the list1 (I need it unchanged in the later part of my code) and also list2 which has other data apart from 'list1' (Want to add data over list1 data). How't that copying possible?

infinite loop
  • 1,309
  • 9
  • 19
  • 2
    A short answer would be that numerical primitives are assigned by value (it is a reference copy, but there is no way of changing its value without creating a copy, google mutable vs immutable in python) almost everything else by reference. – Imanol Luengo May 11 '17 at 06:47
  • 2
    Python FAQ: [Why did changing list ‘y’ also change list ‘x’?](https://docs.python.org/2/faq/programming.html#why-did-changing-list-y-also-change-list-x) – handle May 11 '17 at 06:47
  • You may also be interested in [How do I pass a variable by reference?](http://stackoverflow.com/q/986006/1619432). – handle May 11 '17 at 06:54
  • so, does that mean every value change of `num1` or `num2` will creat a new object rather than overwriting the current memory space? – infinite loop May 11 '17 at 06:55
  • There is no way in python to do `*num1 = 6` (atleast using public interfaces) so assignments will point `num1` to new memory spaces, and *inplace* operators (`+=`) often return *copies* for primitive objects. Other more complex objects behave diffetently and accept reference assignments and inplace modifications, which means you can do proper referencing with them. – Imanol Luengo May 11 '17 at 07:03

3 Answers3

2

Numbers, strings and booleans are primitive types in python. So when you write

num1 = 1
num2 = num1
print num1, num2
num1 += 1
print num1, num2

you see than you can modify num1 independently of num2.

Lists and other complex structures are references. You can imagine that the variable contains their address in memory. So when you write

list1 = [1,2,3,4,5]
list2 = list1
print list1,list2
list1[2:2] = [0,0,0]
print list1, list2

then in the second line you make both variables refer to the same data structure in memory. So when you use one of them to modify this structure, then the changes are visible in both variables.

fafl
  • 7,222
  • 3
  • 27
  • 50
  • I cannot test it but I think booleans are also passed by reference (in fact, python represents False and True as constants and points variables to them). – Imanol Luengo May 11 '17 at 06:56
2

Python variables can be classified as two, mutable and immutable. Read about them.

For simple explanation, consider the following code:

#Integer case
num1 = 1
num2 = num1

#List case
list1 = [1,2,3,4,5]
list2 = list1
list1[2:2] = [0,0,0]

print id(num2)
print id(num1)
print id(list1)
print id(list2)
num1 = 5
print id(num1)

An example output will be:

33124696
33124696
139948056802008
139948056802008
33124600

What happens is that when you do num2=num1, a new variable is created, and it points to the location of num1's value. when num1=5 is performed, 5 is stored in another new memory space, and num1 now points to it.

But this is not the case in lists. For lists, the same memory space will be modified keeping the id(s) the same.

This concept will be difficult to understand if you got started with your programming life with C or C++. But try googling about mutable and immutable variables in python.

Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41
1

num1 and num2 as single types have their values in different parts of memory, so their values are independent one from other.

Lists (and other complex types) uses references when you use the assignment operator =, so the left hand operator of = obtains the address of the right one.

It means that there is only one list in memory and both list1 and list2 share this memory.

MarianD
  • 13,096
  • 12
  • 42
  • 54