On 2014-03-04, at 15:14 , Chris Angelico email@example.com wrote:
On Tue, Mar 4, 2014 at 11:50 PM, Masklinn firstname.lastname@example.org wrote:
On 2014-03-04, at 13:07 , Chris Angelico email@example.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
I don't agree with this, again why would the thunk be evaluated twice? If thunks are added to *delay* expression evaluation (which is what I understood from Steven's messages) surely something akin to Haskell's semantics is simpler to understand and implement. That is, instead of thunks being sugar for:
bar = lambda: expr
they're sugar for
bar = memoize(lambda: expr)
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
Obviously, but why would repeated evaluation of the expression be desirable?
AND it might have unexpected side effects:
bar = `whiskey.pop().drink()`
and you might find yourself underneath the bar before you know it.