Python:how to understand assignment and reference? -


the python code section following lines:

>>> values = [0, 1, 2] >>> values[1] = values >>> values [0, [...], 2] 

why value [0,[...],2],what ...? why value not [0,[0,1,2],2]?

[...] means self-referenced variable (cyclic reference):

>>> values = [0, 1, 2] >>> sys.getrefcount(values) #two references far: shell , `values`   2 >>> values[1] = values     #created reference same object cyclic 1 >>> sys.getrefcount(values) # references increased 3 3 >>> values[1] values  # yes both point same obejct true 

now can modify object using either values or values[1]:

>>> values[1].append(4) >>> values [0, [...], 2, 4] #or >>> values[1][1][1].append(5)  >>> values [0, [...], 2, 4, 5] 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -