Q: is there a way to get Python to give me a warning when there is more than one definition of the same function, conflicting, etc.
(I just wasted a bit of time making edits to two different versions of the same function.)
A small example of conflicting function definitions:
$> cat ./foo2.py
cat ./foo2.py
def foo():
print "1st definition of foo()"
def foo():
print "2nd definition of foo()"
foo()
Executing - Python uses the last definition. (By the way, is that officially defined? It is useful semantics, e.g. when creating functions on the fly in strings and evaling them. It is useful in an interpreter. But it masks bugs in some circumstances, hence my desire to have an optional warning.)
$> python ./foo2.py
python ./foo2.py
2nd definition of foo()
"python -W all" doesn't give a warning:
$> python -W all ./foo2.py
python -W all ./foo2.py
2nd definition of foo()
Neither does "use warnings".
For that matter, python3 does the same as the above (although python3 requires parens around the argument to print :-( ).
pylint DOES report the error
E: 4, 0: function already defined line 2 (function-redefined)
but I have the usual objections to separate lint tools that should be well-known to anyone familiar with the history of programming languages. (E.g. lint is noisy; lint is optional, not always run; lint is optional, e.g. was not installed automatically with python, and required me to do some hacks to get to install (homebrew problems?) - I almost gave up, and if I had given up I would not have had pylint.)
Therefore, even though pylint does find the error, I still ask:
Q: is there any way to get python itself to report an error or warning for multiply defined functions?
===
By comparison, and in response to folks who say "Why would you want to do that in a dynamic language?"
Perl is also a dynamic language. When Perl first came out, it did not have such warnings. But now it does, as people realized that such warnings are good for code quality.
Perl does not warn about multiple definitions by default, but does warn with -Wall or use warning.
$> cat foo2.pl
cat foo2.pl
sub foo { print "foo#1" }
sub foo { print "foo#2" }
foo()
$> perl -Wall ./foo2.pl
perl -Wall ./foo2.pl
Subroutine foo redefined at ./foo2.pl line 2.
foo
No, there's no way to get Python to behave this way natively. A function definition is essentially an assignment to a name, and names in Python can be reassigned at any time. Functions are not treated in any special way compared to integers, strings, etc.
You can decorate all you functions and create a database of symbols and warn on redefinitions.
there's nothing built into the language, but you can use a code-checking tool like pylint (and I believe there are other tools), which will find these kinds of things and warn you about them.