You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CONSTANT=1deffn(x):
globalCONSTANT# <- unnecessary, unidiomatic, and a (small) performance hitreturnx+CONSTANT
The global statement is only required when assigning to a global variable; you can reference outer namespaces without it. I think it'd be nice to have a lint rule that pointed this out, to help newer Pythonistas understand how scoping works.
The text was updated successfully, but these errors were encountered:
I have seen this too and agree it would be good for linters to warn against this. Is it in scope for flake8-bugbear, though? The README says this linter is there to find likely bugs or design problems in your code, and unnecessary global statements aren't bugs, they just do nothing.
a (small) performance hit
There appears to be no runtime hit; your function compiles to the same bytecode with and without the global statement. I guess it makes the initial compilation slightly slower.
The
global
statement is only required when assigning to a global variable; you can reference outer namespaces without it. I think it'd be nice to have a lint rule that pointed this out, to help newer Pythonistas understand how scoping works.The text was updated successfully, but these errors were encountered: