This is a minor thing, but it is puzzling. Here is a code fragment from a project I'm working on:
for device_id in job.devices: device = job.devices[device_id] <loop> del device_id del device
When I run pylint (version 1.6.5, astroid 1.4.9) against this code, I get one warning diagnostic:
Using possibly undefined loop variable 'device_id' (undefined-loop-variable)
Experimenting with Python 2, the loop variable is indeed in the name space of the function I'm running. I get the same result with Python 3, the loop variable is indeed in the function namespace. The reason I'm using 'del' is to avoid name collisions on other sections of code being written at different times -- this project has gone two months already. My "workaround" is to remove the del statement for the loop variable, and leaving the rest of the del statements in for variables inside the loop that (being a ex PL/1 programmer) I would assume would be out of scope, but aren't. I may move this loop into a separate function, and remove the need for the del statements. Comments?
participants (1)
-
Stephen Satchell