仅当函数没有副作用并以确定性方式返回单例(给定其输入)时,结果才会相同。例如:def is_computer_on(): return Truex = y = is_computer_on()要么def get_that_constant(): return some_immutable_global_constant请注意,结果将是相同的,但实现结果的过程将不会:def slow_is_computer_on(): sleep(10) return Truex和y变量的内容相同,但指令x = y = slow_is_computer_on()将持续10秒,而其对应内容x = slow_is_computer_on() ; y = slow_is_computer_on()将持续20秒。这将是几乎如果该函数不具有副作用,并返回一个不可变的以确定的方式相同的(给定其输入)。例如:def count_three(i): return (i+1, i+2, i+3)x = y = count_three(42)请注意,适用上一节中说明的相同捕获。为什么我说差不多?因为这:x = y = count_three(42)x is y # <- is Truex = count_three(42)y = count_three(42)x is y # <- is False好的,使用is有些奇怪,但这说明返回的结果是不一样的。这对于可变情况很重要:如果函数返回可变值,则很危险,并可能导致错误这个问题也得到了回答。为了完整起见,我重播该参数:def mutable_count_three(i): return [i+1, i+2, i+3]x = y = mutable_count_three(i)因为在那种情况下x,并y是同一个对象,做这样的操作x.append(42)对子级意味着两个x并y持有到现在有4个元素的列表的引用。如果该功能有副作用,那将是不一样的考虑到打印的副作用(我认为这是有效的,但也可以使用其他示例):def is_computer_on_with_side_effect(): print "Hello world, I have been called!" return Truex = y = is_computer_on_with_side_effect() # One print# The following are *two* prints:x = is_computer_on_with_side_effect()y = is_computer_on_with_side_effect()它可能会带来更复杂或更微妙的副作用,而不是打印出来,但是事实仍然存在:该方法被调用一次或两次,并且可能导致不同的行为。如果函数在给定其输入的情况下是不确定的,那就会不同也许是一个简单的随机方法:def throw_dice(): # This is a 2d6 throw: return random.randint(1,6) + random.randint(1,6)x = y = throw_dice() # x and y will have the same value# The following may lead to different values:x = throw_dice()y = throw_dice()但是,涉及到时钟,全局计数器,系统的东西,等东西是明智的是非确定性给出的输入,在这些情况下的价值x和y可能发散。