Strange variable scope... is this right?
gradha at iname.com
gradha at iname.com
Sat Jan 13 09:57:20 EST 2001
Hi.
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
This prints:
row
0
1
2
cow
0
1
2
arrow
0
1
2
3
4
I expected python to bark at me like C would, but from what I read in the
tutorials, this seems to be one of those variable scope issues, right?
AFAICS, python will create a different variable scope at both for's,
hence the possibility to reuse the variable name f, since it means a
different thing at each scope, being unavailable from outside it's scope.
Is this a correct deduction? Is this related with the need to declare in
a function global variables so that if they are modified, the modifications
will be seen outside?
If this is right, I would understand that python duplicates globals (and
local variables if they exist) are duplicated at _each_ new scope. Isn't
this too time consuming? How is this possible?
--
Grzegorz Adam Hankiewicz gradha at iname.com - http://gradha.infierno.org
Other web pages: http://glub.ehu.es/ - http://welcome.to/gogosoftware/
More information about the Python-list
mailing list