why x is changed in the following program?

Palpandi palpandi111 at gmail.com
Fri Mar 18 14:55:24 EDT 2016


On Friday, March 18, 2016 at 3:53:58 PM UTC+5:30, maurice... at telecom-paristech.fr wrote:
> from numpy import random
> x=random.randn(6)
> y=x
> y[0]=12
> print x[0]
> 
> 
> 
> Maurice Charbit
> Emeritus Prof.
> mob.: 33 (0)6 5230 1171
> tel.: 33 (0)1 4581 7178
> fax:  33 (0)1 4581 7144
> maurice.charbit at telecom-paristech.fr <mailto:maurice.charbit at telecom-paristech.fr>
> Adresse:
> Institut Mines-Telecom,
> 46, rue Barrault, 75014 Paris

http://stackoverflow.com/questions/6793872/variable-assignment-and-modification-in-python

if 
a = 5
b = a
print id(a) == id(b)
>> False
b = 2
print a
>> 5

In this case we assigning the value to b.

But in the above case, we are assigning the same object reference. Like arrays in c.



More information about the Python-list mailing list