Python references

前端 未结 5 1449
盖世英雄少女心
盖世英雄少女心 2020-11-29 10:00

Can someone explain why the example with integers results in different values for x and y and the example with the list results in x and y being the same object?

<         


        
5条回答
  •  無奈伤痛
    2020-11-29 10:54

    That's because when you have a list or a tuple in python you create a reference to an object. When you say that y = x you reference to the same object with y as x does. So when you edit the object of x y changes with it.

提交回复
热议问题