PyCharm is a hugely popular Python IDE developed by JetBrains. PyCharm supports pretty much anything you can think of when you mention a modern IDE, such as debugging, syntax highlighting, project management, smart prompt, automatic completion, unit testing, version control… In addition to that, PyCharm also provides advanced web development for Django, doing data science with Anaconda and even IronPython.
While working with PyCharm, you might encounter some warning, a few of them are not Python error messages. "Shadows name from outer scope" is one of the most common error message that spread confusion among new users. This article will explain why it happens and what you can do to avoid it.
Why does "Shadows name from outer scope" happens?
Let’s take the code snippets below as our example :
the_list = [1, 2, 3] def data_log(the_list): # Warning: "Shadows 'the_list' from outer scope print(the_list) data_log(the_list)
You can clearly see that in the first line we define
the_list in the global scope, and inside the function
data_log, we reused that name. Reusing names in and out of functions will be referred as "shadowing names" in PyCharm, therefore causes "Shadows name from outer scope". This is just a warning and doesn’t make your code unable to run.
While there’s nothing wrong with the example and it runs just fine, it can be the beginning of strange behaviour if you continue to ignore the error message.
data_log takes in not one but a few more arguments, and the inner logic of it gets more complex. You decide to manually rename
array_object, but leaves a few miss here and there.
If you run the code again, it might just work, but the results will certainly be strange. That is because now
the_list refers to the global object,
array_object refers to the local one, they are different and you mixed them back and forth inside your function.
Avoid "Shadows name from outer scope"
Now that we know the reason behind the warning, the solution is quite simple : You need to avoid reusing names in your code.
Doing so will not only reduce weird behaviour in your code, but also make debugging easier, since you would end up with
NameError if Python cannot find a global name or local name.
One more thing to remember is that you should not "shadow" modules, classes and functions the same way you shadow names, too.
This is how our example will look like if it doesn’t shadow names :
the_list = [1, 2, 3] def data_log(a_list): # Warning: "Shadows 'the_list' from outer scope print(a_list) data_log(the_list)
Or you can just move the global variables to another function like so :
def data_log(a_list): print(a_list) def main(): the_list = [1, 2, 3] data_log(the_list)
You might also want to read our guide on how to fix IndentationError: unindent does not match any outer indentation level since it is another common Python error which has confuses a lot of people. We’ve also covered how to fix locale.Error: unsupported locale setting once and for all, which might be a good read for new Python developers. We hope this article will help you solve your problem and write better, cleaner, error-free code.