-2

I started to learn python but find something annoying

In [82]: a = b = 8                                                                                                            
In [83]: id(b)                                                                                                                
Out[83]: 94772909397088
In [84]: id(a)                                                                                                                
Out[84]: 94772909397088
In [86]: id(8)                                                                                                                
Out[86]: 94772909397088

The result is anti-intuitive

b pointing 8 and a pointing b, b hold 8's address, a hold b's address. but they return the same result.

What's more, If change b

In [88]: b = 9                                                                                                                
In [89]: a                                                                                                                    
Out[89]: 8

b changed to 9 but a does not change.

So it make no difference `a= b= c= 9' or 'c=b=a=9'

How could wrap brain on this intuitively?

Alice
  • 1,360
  • 2
  • 13
  • 28
  • 5
    `a` doesn't point to `b`. You can't assign variables to variables, you can only assign the same object to a new name. `a = b = ...` means: assign the result of the expression to `a`, and then assign the same object to `b`. – Martijn Pieters Mar 27 '19 at 15:24
  • Oh, Master, My Master, Thank you @MartijnPieters – Alice Mar 27 '19 at 15:27
  • 2
    See https://nedbatchelder.com/text/names.html for a great explanation for how Python names work. – Martijn Pieters Mar 27 '19 at 15:27
  • an intuitive understanding, a variable does not exist in python, because it has never been defined and it has no memory address for itself. `variable` is vacuum in python. @MartijnPieters – Alice Mar 28 '19 at 00:02

1 Answers1

-2

if you want to get a figure changed,you can use the function copy.

import copy
aList=[[1,2],3,4]
bList=copy.copy(aList)
print aList
print bList

print id(aList)
print id(bList)

aList[0][0]=5

print aList
print bList
HT.haung
  • 41
  • 4