On Mon, Dec 8, 2014 at 3:44 PM, Phil Frost <ind...@bitglue.com> wrote: > On Mon, Dec 8, 2014 at 3:21 PM, Ian Cordasco <graffatcolmin...@gmail.com> > wrote: >> >> I'm not sure in what case code like what Skip shared wouldn't result >> in an error: >> >> for i in range(1, 10): >> for i in ['foo', 'frob', 'bob', 'bogus', 'smogus']: >> do_stuff(i) >> do_other_stuff(i) > > > A slight variation that is probably correct: > > for i in range(1, 10) > do_other_stuff(i) > for i in ['foo', 'frob', 'bob', 'bogus', 'smogus']: > do_stuff(i) > > The difference here being that the outer `i` is used before it's reassigned > by the inner loop. We could only emit a warning if the loop variable is > re-defined if it's not yet used, but then what about this: > > # https://www.youtube.com/watch?v=jrzUsHNGZHc > for _ in xrange(3): > for _ in xrange(3): # `_` is unused and redefined, but who cares? > knock() > say "Penny" > > We might consider re-using a loop variable outside the loop as the problem, > but then what about: > > for thing in some_things(): > if is_the_one_im_seeking(thing): > break > else: > raise CouldntFindTheThingException > frob(thing) > > I agree, it would be good to catch this error, but I haven't thought of a > way to do it that doesn't run afoul of false positives. > > _______________________________________________ > code-quality mailing list > code-quality@python.org > https://mail.python.org/mailman/listinfo/code-quality >
Your second example is moot because PyFlakes already silences certain warnings if _ is the variable name. I also think you're blurring the lines too much. Re-using the loop variable outside a loop is common (and often intentional). This doesn't even relate to the original request or its validity. _______________________________________________ code-quality mailing list code-quality@python.org https://mail.python.org/mailman/listinfo/code-quality