So I've got some code similar to this (this code reproduces my question, even if it doesn't do anything useful):
def fn():
l = ["foo", "bar", "baz"]
print map( lambda f : len(f), l )
for i, f in enumerate(l):
print i, f
And PyCharm is reporting that my declaration of lambda f
"Shadows name f from outer scope". This appears to be because of the variable being reused in the call to enumerate()
in the following line.
The code works as expected, but what's happening here? Am I correct in thinking that Python is deciding that f
is a local variable in fn
and warning me that the use of f
inside the lambda isn't going to be the same f
defined locally - even though the lambda makes use of the variable name f
first?
Other than renaming variables, is there a best practice for avoiding this - or should I just use my judgement and ignore the warning in this instance?