[Python-projects] avoiding 'possibly undefined loop variable' warnings

Duncan Gibson duncan at thermal.esa.int
Thu Jun 22 12:05:52 CEST 2006


Hi there! I've just joined the list, but I have browsed the archive...

I'm a great believer in running your code through as many parsing
tools as possible, because they all spot different problem areas,
and refactoring to eliminate as many warning messages as possible.

So in addition to [python], pychecker and epydoc, I've downloaded
and started to use pylint-0.11.0 (with astng-0.16.0, common-0.15.0)
under Python 2.4.2 on Lunar Linux. And very useful it has been too!

However, I've come across an idiom that throws up a warning and I
can't really understand why. I've reduced it down to the following:

#============================
value = 'I'
for value in ['X', 'Y', 'Z']:
    print 'looping'

print value

if value in ['A', 'B', 'C']:
    print 'Impossible!'
#============================

as expected, python test.py gives:
looping
looping
looping
Z

pylint gives warnings for the 'print value' and 'if value in' lines
W:  5: Using possibly undefined loop variable 'value'
W:  7: Using possibly undefined loop variable 'value'

I added the redundant "value = 'I'" statement, and the "Z" in the
output proves that "value" is defined.

Apart from disabling W0631, how can I avoid this warning?

Cheers
Duncan


More information about the Python-Projects mailing list