[Tutor] Language truce
Remco Gerlich
scarblac@pino.selwerd.nl
Tue, 3 Jul 2001 01:46:42 +0200
On 0, D-Man <dsh8290@rit.edu> wrote:
> On Thu, Jun 28, 2001 at 02:20:58PM -0700, Danny Yoo wrote:
> | One reason that 'for/in' might be preferred is because we are guaranteed
> | that the loop will always terminate: there can only be a finite number of
> | elements in a list. That's one big advantage of a loop construct that
> | goes along elements --- theoretically, we'll never get into infinite
> | loops.
>
> Almost.
>
> l = range( 1 )
> for i in l :
> print l
> l.append( i+1 )
>
> Don't do this! It's just as bad in C or any other language --
> mutating a collection while iterating over it is just a bad idea.
Or even:
class InfiniteList:
def __getitem__(self, i):
return i
for i in InfiniteList():
print i
(well, it'll end when i doesn't fit into an integer anymore)
--
Remco Gerlich