Redefining name 'e' from outer scope (line 110)
116 os.getcwd(), "Resources/Images/Saved_fractals/"
117 )
118 )
119 except Exception as e:120 print("Error: ", e)
121 checked_ez_button.create_button()
122
Redefining name 'e' from outer scope (line 139)
140 print("Error: ", e)
141 try:
142 os.system("xdg-open " + "CONFIG.ini")
143 except Exception as e:144 print("Error: ", e)
145 if any(
146 complex_button.check_hover(mouse_x, mouse_y)
Redefining name 'e' from outer scope (line 75)
83 ),
84 ]
85 )
86 except Exception as e: 87 print("Error:", e)
88
89 if checked_ez_button.name == "btnExport":
Description
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
Bad practice
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
Preferred:
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Bad practice
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
Preferred:
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()