编码时如何阻止自己覆盖Python函数? [英] How to stop myself overwriting Python functions when coding?
问题描述
list = [)在我的Python代码中追踪错误时,始终头痛的源头是看似无害的代码片段, 'a','b','c','c']
list(set(list))
这个失败是因为我用变量列表覆盖了函数list()。
显然是一个人为的例子,但问题在于Python很高兴让我用变量覆盖内置函数。我意识到这是Python中的一个重要特性,但如果解释器在我的代码中执行时会提醒我,我通常不会这么做,所以我非常喜欢它。
任何人都可以提出一个解决方案(除了只是更小心) - 因为我一直绊倒这个问题吗? 解决方案
使用突出显示语法的文本编辑器,将突出显示与其余代码不同颜色的关键字。
A source of constant headache when tracking down bugs in my Python code are seemingly innocuous snippets like this:
list = ['a', 'b', 'c', 'c']
list(set(list))
This fails because I've overwritten the function list() with the variable list.
A contrived example obviously, but the point is Python happily lets me overwrite built-in functions with variables. I realise this is a crucial feature in Python but I would quite like it if the interpreter would warn me when I do it in my code as I usually don't mean to do this.
Can anyone suggest a solution (other than just being more careful) - as I keep tripping over this problem?
Use a syntax-highlighting text editor that will highlight keywords in a different color from the rest of the code.
这篇关于编码时如何阻止自己覆盖Python函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!