为多重定义的函数寻求 python 警告 [英] Seek python warning for a multiply defined function

查看:62
本文介绍了为多重定义的函数寻求 python 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:当有多个相同函数的定义、冲突等时,有没有办法让 Python 给我一个警告.

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()

执行 - Python 使用最后一个定义.(顺便说一句,这是官方定义的吗?这是有用的语义,例如在字符串中动态创建函数并对其进行评估时.它在解释器中很有用.但它在某些情况下掩盖了错误,因此我希望有一个可选的警告.)

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" 没有给出警告:

"python -W all" doesn't give a warning:

$>  python -W all ./foo2.py
python -W all ./foo2.py
2nd definition of foo()

使用警告"也不行.

就此而言,python3 与上述相同(尽管 python3 需要在参数周围使用括号来打印 :-( ).

For that matter, python3 does the same as the above (although python3 requires parens around the argument to print :-( ).

pylint 确实会报告错误

pylint DOES report the error

E:  4, 0: function already defined line 2 (function-redefined)

但我通常反对分离 lint 工具,任何熟悉编程语言历史的人都应该知道这些工具.(例如 lint 很吵;lint 是可选的,并不总是运行;lint 是可选的,例如不是用 python 自动安装的,并且需要我做一些黑客才能安装(自制问题?) - 我几乎放弃了,如果我已经放弃了,我不会有 pylint.)

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.)

因此,即使pylint确实发现了错误,我还是问:

Therefore, even though pylint does find the error, I still ask:

问:有什么办法可以让python本身为多重定义的函数报告错误或警告?

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 也是一种动态语言.Perl刚出来的时候,还没有这样的警告.但现在确实如此,因为人们意识到此类警告对代码质量有益.

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 不会针对多个定义发出警告,但会使用 -Wall 或使用警告进行警告.

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

推荐答案

不,没有办法让 Python 以本机方式运行.函数定义本质上是对名称的赋值,Python 中的名称可以随时重新赋值.与整数、字符串等相比,函数没有以任何特殊方式处理.

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.

这篇关于为多重定义的函数寻求 python 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆