【发布时间】:2013-06-20 13:48:30
【问题描述】:
list(Case 1) 的属性如何调用以使print y 的输出与Case 2 不同?
# Case 1: using a list as value
>>> x = ["one", "two", "three"]
>>> y = x
>>> x[0] = "four"
>>> print x
["four", "two", "three"]
>>> print y
["four", "two", "three"]
# Case 2: using an integer as value
>>> x = 3
>>> y = x
>>> x = x + 1
>>> print x
4
>>> print y
3
编辑:
为了表明这种行为与列表是可变的和字符串不是无关的,代替案例 2,可以给出以下案例:
>>> x = ["one", "two", "three"]
>>> y = x
>>> x = x + ["four", "five"]
>>> print x
["four", "two", "three", "four", "five"]
>>> print y
["four", "two", "three"]
【问题讨论】:
-
请使您的问题的标题可读。避免将代码放在那里,最好尝试总结您的问题。
-
如果有人不知道引用与值的关系,将作为这个问题的一个好标题吗?
-
对了,如果你不知道房产的名称,你会如何解释 Ivaylo?
-
关于某人对标题所做的更改:它可能更易于阅读,但描述性较差。我真的不认为这是一种改进。如果有人和我有同样的问题,我看不出这个标题会如何引导他进入这个话题。所以这个问题很容易再次被重复。
标签: python