-1

I'm trying to learn coding through some tutorials and encountered something curious as I'm going over operators. The basic ones I get, but some others (like *= here) throw me off.

a = 1 
b = a 
a *= 2 
print(a)
print(b)

Output

2
1

But when a is an array, this happens:

a = np.array([1, 2, 3, 3, 5, 5])
b = a
a *= 2
print(a)
print(b)

Output

[2 4 6 6 10 10]
[2 4 6 6 10 10]

Any insight as to why this happens? I can't really find much about it. The only thing I came across was "coercion rules"; does the second instance happen because a is being assigned to an array but then to an integer? Or is it a matter of the print statement order? It's probably trivial but I'm just curious, thanks!

spokati
  • 45
  • 5
  • 2
    See https://stackoverflow.com/questions/3059395/numpy-array-assignment-problem. – JASON G PETERSON Aug 28 '21 at 05:04
  • Are you asking why ```a``` and ```b``` have the same values, or why ```a * 2``` doubled the values in ```a``` ? – sj95126 Aug 28 '21 at 05:07
  • why a and b have the same values in the second instance (but not the first) – spokati Aug 28 '21 at 05:09
  • 3
    @spokati in both, initially a and b and referencing the same object. In the first ints handle `*=` by returning a new integer object so a and b are now different. In the second numpy arrays handle `*=` by modifying the array in place and returning the same object so a and b are still referencing the same (modified) array – Iain Shelvington Aug 28 '21 at 05:13
  • In addition to tutorials, you should have a decent python book, and the `numpy` docs, starting with https://numpy.org/doc/stable/user/absolute_beginners.html – hpaulj Aug 28 '21 at 05:30
  • As a general rule in Python, don't use `b=a`. `b` references the same object as `a`. There's no (implied) copy. Occasionally in interactive coding I'll do `x=long_variable_name`. Otherwise such an assignment is not useful, especially if `a` is a mutable object. – hpaulj Aug 28 '21 at 15:04
  • Thank you for all of the suggestions and insight! – spokati Aug 29 '21 at 00:32

1 Answers1

1

The Simple answer to your question is,

There are two kinds of objects in Python: Mutable objects and Immutable objects. The value of a mutable object can be modified in place after it’s creation, while the value of an immutable object cannot be changed.

Immutable(Not Modifiable) Object: int, float, long, complex, string tuple, bool

Mutable(Modifiable) Object: list, dict, set, byte array, user-defined classes.

So,here in your case firstly a and b are immutable objects as it belongs to class int in python,"b=a" means b is pointing towards a address,and as you update value of a to a*=2, the value is store on new memory location, but b is still pointing towards older address, thats why b is not showing changed value of a.

For more understanding of memory management in python, please read this blog, thankyou :) https://medium.com/@tyastropheus/tricky-python-i-memory-management-for-mutable-immutable-objects-21507d1e5b95

Niraj Jain
  • 26
  • 6