Warning: Shadows name X from outer scope in PyCharm

Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min


# Warning: Shadows name X from outer scope in PyCharm

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)

shadows name from outer scope

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.

The warning is shown because the name of the parameter (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)

rename function parameter

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)

rename outer scope variable to resolve warning

I renamed the outer scope variable to website and the function's parameter is named site, so they no longer clash.

# Function parameter/variable sharing the same name as an outer scope variable

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.

You cannot access the `site` variable from the outer scope inside the 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.

# Disabling the "Shadowing names from outer scopes" warning in PyCharm

If you want to disable the "Shadowing names from outer scopes" warning in PyCharm:

  1. Press Ctrl + Alt + S or click on File in the top menu and then select Settings.

  2. Click on Editor, then Inspections and search for shadowing names.

  3. Uncheck the Shadowing names from outer scopes checkbox and click on Apply and OK.

disable shadowing names from outer scope in pycharm

# Wrapping the outer scope variable in a function

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.

# Using a trailing underscore to differentiate between the two variables

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.