2 ms·
No, but even a use strict type clause wouldn't have helped in this case, since the entire syntax was valid (volme was being properly initialized as part of the
by falcolas 10y ago
No, but even a use strict type clause wouldn't have helped in this case, since the entire syntax was valid (volme was being properly initialized as part of the loop definition). The error occurred because 'volume' was considered to be part of the function scope, despite being defined as part of the for loop scope.
To me, it's a good reason to use different loop variable names for successive loops. A good practice to use in any language, since it helps keep loops distinct to your mind as well.
for volume in get_volumes():
print volume
for other_volme in get_other_volumes():
print other_volume
The second loop throws a runtime error and an error level linting alert.
- Mithaldu 10y agoIt would help if it restricted variables auto-vivified in a loop declaration to the body of the loop itself, instead of making it available for the entire parent scope. I'm used to working with languages that behave like that. I'm actually a little surprised something like that hasn't been attempted yet for Python. Thanks for the answer!