On Tue, Mar 4, 2014 at 11:50 PM, Masklinn <masklinn@masklinn.net> wrote:
On 2014-03-04, at 13:07 , Chris Angelico <rosuav@gmail.com> wrote:
Most people expect that:
foo = bar assert foo is bar
to be a safe assumption, but if bar is a thunk, then it's getting evaluated separately in each of those
Why? Either it's forced during assignment or both names map to the same thunk, and are both forced when any of them is.
That could be during the identity check, but since both names refer to the same thunk they can only yield the same value, so the identity check needs not force the thunk. An equality test would likely force the thunk.
Most certainly not. Try this: bar = `[1,2,3]` foo = bar spam = bar assert foo is spam Here's the evaluated version: foo = [1,2,3] spam = [1,2,3] assert foo is spam You can try this one out directly. They're not going to be the same object - they'll be two separate lists. They will be equal, in this case, but there's no guarantee of that either: bar = `random.random()` Separate evaluation of the same expression isn't guaranteed to have the same result, AND it might have unexpected side effects: bar = `whiskey.pop().drink()` and you might find yourself underneath the bar before you know it. ChrisA