Greg Ewing wrote:
Nick Coghlan wrote:
Since the language reference is actually silent on the topic of caching the bound method when iterating over an object,
Since it's silent about that, if you write a for-loop that relies on presence or absence of cacheing behaviour, the result is undefined. The behaviour of yield-from on the same iterator would also be undefined.
It's meaningless to talk about whether one undefined construct has the same semantics as another.
I agree that would be true in the absence of an accepted reference implementation (i.e. CPython) that doesn't cache the bound methods (hence allowing one to play games with the next() method definition while looping over an iterator).
If I understand Guido's last message correctly, this is one of the cases where he would like the existing behaviour of the CPython implementation to be the defined behaviour for the language as well.
P.S. I created http://bugs.python.org/issue5739 as a documentation bug pointing back to this email thread in relation to whether it is OK for a Python implementation to cache the next() method lookup in a for loop.
P.P.S. OK, stepping away from the computer and going on vacation now... :)