How to name variable correctly to avoid warning like "Shadows name from outer scope" in Python

Question:

I use PyCharm for my python program, and I wrote codes below:

def get_files_name():
    root_dir = "/Volumes/NO NAME/"
    for root, ds, fs in os.walk(root_dir):
        for f in fs:
            print(os.path.join(root_dir, f))


get_files_name()
for root, ds, fs in os.walk(other_dir):
    pass

So I get a Warning text like “Shadows name ‘ds’ from outer scope”.
I know the effect of scope, but I still want to use the same code format like “for root, ds, fs in ….” at inner or outer of scope.

I have searched PEP8, however, I still don’t know how to name variable in function normatively.

Could you give me some suggestion?

Asked By: GoldenMan

||

Answers:

In general: just ignore the warning. It’s just a warning, not an error. You have use for both global and local names that happen to match.

However, I’d not put an os.walk() call at global scope anyway. I’d rather put that into a function too, which has the happy side-effect of the names you used no longer being globals.

For example, you could use a main() function:

def main():
    get_files_name()
    for root, ds, fs in os.walk(other_dir):
        pass

if __name__ == '__main__':
    main()

Generally speaking, you don’t want to leave loop names like root, ds, fs as globals in your module anyway. Those are implementation details, and should not become part of the public API of a module. If you have to use a for loop like that at the global scope, use _ single-underscore prefixes on the names and consider deleting them after the loop with del:

for _root, _ds, _fs in os.walk(other_dir):
    # do something with the files or directories

# clean variables for the loop that are not part of the API
del _root, _ds, _fs
Answered By: Martijn Pieters

That warning shadows name XX from outer scope is not a PEP8 problem, but an actual warning from Pycharm to tell you that reusing variable names in that way is a bad idea. In other words, this is not a code style problem, but something that may bring problems later on in larger programs.

My suggestion would be to, well, avoid reusing variable names when possible. Typing this:

for root_path, directory_name, file_name in os.walk(root_dir):

doesn’t take a lot of time, and would avoid undesirable side-effects in the future.

Still, if for any reason you absolutely need to reuse variable names and want to get rid of the warning message, you can disable it in Pycharm (Preferences -> Editor -> Code Style -> Inspections -> shadowing names from outer scopes). But this is usually a bad idea.

Answered By: carrdelling

If your names repeats use “_” to avoid such warnings.It’s a common practice.

def get_files_name():
    root_dir = "/Volumes/NO NAME/"
    for root, _ds, fs in os.walk(root_dir):
        for f in fs:
            print(os.path.join(root_dir, f))

get_files_name()
for root, _ds, fs in os.walk(other_dir):
    pass
Answered By: Alex Delarge

I advise heeding the warning and changing your code.

This warning exists for a few reasons. One is to prevent users from accidentally modifying variables when they shouldn’t be. This error, when it happens, can be very difficult to debug. Often it’s a silent error. Even when it causes an exception, it is often difficult to isolate the cause. Reusing a variable name can look very normal and clean. A second reason, more minor, is to improve readability and the ability to refactor code, like renaming things.

Either use different names for variables and/or modularize your code more. (if something reuses a variable name, consider whether it should be in a different module.)

Answered By: william_grisaitis