Strange variable scope... is this right?

Remco Gerlich scarblac at pino.selwerd.nl
Sat Jan 13 19:26:14 EST 2001


gradha at iname.com <gradha at iname.com> wrote in comp.lang.python:
> In another attempt to DTW (Dominate The World), I wrote an obfuscated loop
> just to see the scope behaviour of Python:
> 
> f = ["row", "cow", "arrow"]
> for f in f:
> 	print f
> 	for f in range (len (f)):
> 		print f

When 'for f in f:' evaluates, the expression after 'in ' is evaluated,
and it results in the list. The for loop stores a reference to the list,
and sets f to the first element. In later iterations, the for loop still
remembers the list but otherwise it is gone.

After the loop, f points to the last thing it was set to (4, probably, from
the second loop). The list is gone.

-- 
Remco Gerlich



More information about the Python-list mailing list