On Mon, Dec 8, 2014 at 1:24 PM, Ian Cordasco <graffatcolmin...@gmail.com> wrote:
> > On Dec 8, 2014 12:17 PM, "Skip Montanaro" <skip.montan...@gmail.com> > wrote: > > > > Using the same variable in nested loops within the same function seems > at least as serious as a shadowed global (or builtin) name. > > I agree. I think this belongs in PyFlakes (as far as flake8 is concerned) > but Phil may disagree. > Maybe. As a matter of principle, Pyflakes should only emit a warning for things that it is sure is an error. Currently, pyflakes only emits a warning if you redefine an import without ever using it. For example: import x def f(): x = 2 print x <stdin>:1: 'x' imported but unused <stdin>:3: redefinition of unused 'x' from line 1 It does not do this for other kinds of definitions: x = 1 def f(): x = 2 print x [no errors] Also note that as long as the imported "x" is used somewhere, then there are no warnings, even if "x" is shadowed in some scopes: import x def f(): x = 2 print x def g(): print x [no errors] The reasoning here is that if you import a thing and then redefine it without using it ever, that's just the same as importing a thing and not using it. However, I can come up with use cases for shadowing variables from function scope or in nested loops. For example, event-based libraries frequently expect callbacks that take one parameter which the callback may or may not care about. Or, we may use a for loop to do something a number of times without caring about the iteration count. I tend to use "_" for these sorts of things, a convention I got from lisp. def thingHappened(_): # don't care about the thing that happened, just that it happened class Sheldon: def visit_penny(self): for _ in xrange(3): for _ in xrange(3): knock() say('penny') I wouldn't want a warning in these cases, as they are legitimate and correct. Can you think of any additional reasoning we could apply that would catch your case, but not these?
_______________________________________________ code-quality mailing list code-quality@python.org https://mail.python.org/mailman/listinfo/code-quality