0

currently I'm working on a Python project in PyCharm and often get the warning "Shadows name 'foo' from outer scope", e.g. in this minimal example:

def print_foo(foo):
    print(foo)

foo = "Hello"
print_foo(foo)

I understand the sense behind this, but since I like to have a green check mark in the top right corner, I could rewrite the script like this to stop getting the warnings:

def print_foo(foo):
    print(foo)

def main():
    foo = "Hello"
    print_foo

main()

However, I mostly run the script via Alt-Shift-E in the console and unfortunately, due to the code change, I can no longer access the variables, since they are now in the main() function and not in the script directly (if you can put it that way).

Are there any other solutions for this where I no longer get the warnings (without simply suppressing them) and can still access the variables after running the code (preferably all of them of course, but the ones from main() would be enough for me).

Thanks!

Schicko
  • 15
  • 5
  • I don't use PyCharm, but could you not change the name of your input parameter in "def print_foo(foo):" to "def print_foo(foo_parm):" and then execute "print(foo_parm)" or is it critical to keep the variable names to a minimum? – NoDakker Aug 25 '22 at 19:23
  • This would be possible, but it is a variable that is used very often and by many with the same name. Therefore I would like to use the same name in the main (or script directly) and in the function. – Schicko Aug 27 '22 at 00:02
  • Assign the variables outside those scopes or use variable watches. But it goes against the logic of using Python and it doesn't use the IDE's tools. The aim here is altogether a bad practice. – bad_coder Sep 22 '22 at 06:26

0 Answers0