Last updated: Apr 11, 2024
Reading time·3 min
The PyCharm warning "Shadows name 'X' from outer scope" is shown when you have a parameter of a function or a variable defined in a function that shadows a variable defined in the outer scope.
To resolve the issue, rename the variable or parameter so it doesn't clash with the one from the outer scope.
Here is an example of when the warning is shown.
site = 'bobbyhadz.com' # ⛔️ Shadows name 'site' from outer scope def print_site(site): print(site) # bobbyhadz.com print_site(site)
We have a variable named site
that is defined in the global scope.
The print_site
function takes a parameter named site
and prints it.
site
) is the same as the name of the variable (site
) in the outer scope.This causes the two variables to clash.
One way to resolve the issue is to rename the function parameter (or the site
variable from the outer scope).
Here is an example that renames the function parameter.
site = 'bobbyhadz.com' def print_site(website): print(website) # bobbyhadz.com print_site(site)
I renamed the function parameter from site
to website
to resolve the issue.
The name of the parameter no longer shadows the variable from the outer scope so the issue is resolved.
You can also rename the global variable to resolve the warning.
website = 'bobbyhadz.com' def print_site(site): print(site) # bobbyhadz.com print_site(website)
I renamed the outer scope variable to website
and the function's parameter is
named site
, so they no longer clash.
When the function's parameter shares the same name as the outer scope variable, the outer scope variable becomes inaccessible (directly) inside the function.
Here is a code sample that better illustrates this.
site = 'bobbyhadz.com' def print_site(): # ⛔️ Shadows name 'site' from outer scope site = 'google.com' print(site) # google.com print_site()
We declared a site
variable in the outer scope and then declared a variable
with the same name inside the function.
When we pass the site
variable to the print()
function, the variable from
the local scope gets printed.
print_site
function because the local variable with the same name shadows it.To resolve the issue, you either have to rename the global variable or the local variable.
site = 'bobbyhadz.com' def print_site(): website = 'google.com' print(website) # google.com print_site()
Now the names of the outer scope and local variables no longer clash, so the issue is resolved.
If you want to disable the "Shadowing names from outer scopes" warning in PyCharm:
Press Ctrl
+ Alt
+ S
or click on File in the top menu and then
select Settings.
Click on Editor, then Inspections and search for shadowing names.
Uncheck the Shadowing names from outer scopes checkbox and click on Apply and OK.
An alternative way to resolve the warning is to wrap your outer scope variable in a function.
def main(): site = 'bobbyhadz.com' print_site(site) def print_site(site): print(site) main()
The site
variable is no longer defined in the global scope so it doesn't
shadow the parameter of the print_site
function.
A commonly used convention that you can use to solve the error is to add a trailing underscore after the name of the variable (or the name of the function parameter).
site = 'bobbyhadz.com' def print_site(site_): print(site_) # bobbyhadz.com print_site(site)
The function parameter is named site_
instead of site
.
Now the two names don't clash so the warning is no longer shown.
I've also written a detailed guide on how to use global variables in Python.
You can learn more about the related topics by checking out the following tutorials: