Reputation: 8788
Is having a x
as both a module-level variable name and a function argument name a bad practice?
x = 2
def f(x):
print x
f(x)
I am asking this because pylint complains about it:
W: 3, 6: Redefining name 'x' from outer scope (line 1) (redefined-outer-name)
Upvotes: 0
Views: 161
Reputation: 76997
No, it is not, and that is why you are seeing a Warning (W) and not an Error (E).
In general, it depends on your use case. If for example, you have an alternate variable name which can similarly convey the same meaning as your current variable name, it would be better to use it to avoid unnecessary confusion. For the sample in your code, you can very easily use:
def f(n):
print n
The unnecessary confusion could be that you indeed wanted to use the global variable x, or that you might end up comparing values of x from different scopes and ending up debugging why their values are not same.
But if using a previously defined variable name in another scope is the best way to convey the information that the variable is supposed to convey, then go with it.
Upvotes: 1