While working on a large enough python file, I have accidentally redefined a function in the global scope. I would appreciate it if the python interpreter could warn me in those cases.
Imagine you start with this code (version 1):
#!/usr/bin/env python
... lots of code ...
def foo(version):
if version == 1:
return "Correct"
return "Oops!"
... lots more code ...
print foo(1)
Which works properly:
Correct
And then you want to change some things, and call it version 2. You rewrite the foo function, but you either don't realize the old one existed, or you forget to delete it. You end up with this:
#!/usr/bin/env python
def foo(version):
if version == 2:
return "Correct"
return "Oops!"
... lots of code ...
def foo(version):
if version == 1:
return "Correct"
return "Oops!"
... lots more code ...
print foo(2)
Which doesn't work so well:
Oops!
I know python allows code like this:
def monkey():
return "patching"
monkey = "is"
def monkey():
return {"really": "fun"}
But it seems like using "def" that way is poor practice.
Is there any way I can get this sort of behavior:
#!/usr/bin/env python --def-strict
def foo():
pass
def foo():
pass
Results in:
Traceback (most recent call last):
File ..., line 3, in <module>
NameError: name 'foo' is already defined