Shadows name xyz from outer scope

Question:

I am using pycharm and it lists out all the errors/warnings associated with the code. While I understand most of them I am not sure of this one “Shadows name xyz from outer scope”. There are a few SO posts regarding this: How bad is shadowing names defined in outer scopes? but then they seem to be accessing a global variable.

In my case, my __main__ function has a few variable names and then it is calling another function sample_func which uses those variable names again (primarily the loop variable names). I am assuming because I am in a different function, the scope for these variables will be local, however the warning seem to suggest otherwise.

Any thoughts? For your reference here is some code:

def sample_func():
    for x in range(1, 5):  --> shadows name x from outer scope
        print x

if __name__ == "__main__":
    for x in range(1, 5):
        sample_func()
Asked By: The Wanderer

||

Answers:

It is just a warning, as explained in the linked question there are times when it can cause issues but in you case x is local to your function. You are getting the warning because of the x inside your if __name__ == "__main__": being in globals. It won’t have any effect on the x in your function so I would not worry about the warning.

Answered By: Padraic Cunningham

The code inside of your if branch of your main function is actually in scope when you’re inside of sample_func. You can read from the variable x (try it out). This is okay as you don’t really care about it so you have a few options to move forward.

1) Disable shadowing warnings in pycharm. Honestly this is the most straightforward and depending on how experienced of a coder you are it probably makes the most sense (if you’re relatively new I would not do this though.)

2) Put your main code into a main function. This is probably the best solution for any production level code. Python is very good at doing things the way you want to do them so you should be careful not to fall into traps. If you are building a module, having lots of logic at the module level can get you into sticky situations. Instead, something like the following could be helpful:

def main():
    # Note, as of python 2.7 the interpreter became smart enough
    # to realize that x is defined in a loop, so printing x on this
    # line (prior to the for loop executing) will throw an exception!
    # However, if you print x by itself without the for loop it will
    # expose that it's still in scope. See https://gist.github.com/nedrocks/fe42a4c3b5d05f1cb61e18c4dabe1e7a
    for x in range(1, 5):
        sample_func()

if __name__ == '__main__':
    main()

3) Don’t use the same variable names that you’re using in broader scopes. This is pretty hard to enforce and is kinda the opposite of #1.

Answered By: Ned Rockson

The warning is about the potential danger you are introducing by re-using these names at inner scopes. It can cause you to miss a bug. For example, consider this

def sample_func(*args):
    smaple = sum(args) # note the misspelling of `sample here`
    print(sample * sample)

if __name__ == "__main__":
    for sample in range(1, 5):
        sample_func()

Because you used the same name, your misspelling inside the function does not cause an error.

When your code is very simple, you will get away with this type of thing with no consequences. But it’s good to use these “best practices” in order to avoid mistakes on more complex code.

Answered By: krethika

I know this is an old thread and this isn’t appropriate for the problem the asker was trying to find out about, but I was searching for an answer to why PyCharm was showing me a ‘Shadows name from outer scope’ message on a complex if/elif statement block…

It turns out I had capitalised some global variable names at the start of the function but used lower case in my if/elif block much further down in the function.

School boy error I know, but once I corrected this, the ‘Shadows name from outer scope’ message in PyCharm disappeared and the variables stopped showing as greyed out…

So the lesson I learned is that this PyCharm message may be caused by something as simple as a upper/lower case error in a variable name…

I only realised the problem while I was breaking the function into three functions to see if this would remove the ‘Shadows…’ error, as I was thinking I had an issue with indentation and this was causing the problem!

This may help another newbie who is scratching their head wondering why they are getting this error 🙂

Answered By: Mark Smith

I was running into this warning for an argument in a method named year, but no other variable was sharing that name. I then realized that it was because of the line from pyspark.sql.functions import * which was importing a year variable. Changing this to only import the functionality we needed go rid of the warning.

Answered By: David Baucum
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.