如果我理解正确,最好不要将表达式用于已经是Python中的全局函数的局部变量。所以我相信这个
list = [1,2,3]
被推荐赞成
mylist = [1,2,3]
因为list
已经是Python中的内置对象而mylist
不是。但是,我并不总是确定是否应该使用某种表达方式(例如dir
,num
或cnt
)。是否有任何全面的字符串概述我最好避免命名局部变量?
答案 0 :(得分:5)
基本上,避免所有these。所有这些都在__builtin__
模块中(Python 3中的builtins
)。
答案 1 :(得分:3)
要避免的名称是关键字(这会给你一个错误,因此很容易发现)和内置,这将被静默掩盖。这是用于测试错误名称的代码片段:
from keyword import kwlist
def bad_name(name):
return name in dir(__builtins__) + kwlist
...这里是一个列表(对于Python 3.3):
内置功能,类型等
abs all any ascii
bin bool bytearray bytes
callable chr classmethod compile
complex copyright credits delattr
dict dir divmod enumerate
eval exec exit filter
float format frozenset getattr
globals hasattr hash help
hex id input int
isinstance issubclass iter len
license list locals map
max memoryview min next
object oct open ord
pow print property quit
range repr reversed round
set setattr slice sorted
staticmethod str sum super
tuple type vars zip
CamelCase中的任何内容(如内置异常)或以双下划线开头都不在上面的列表中,因为您不应该使用它们。
<强>关键字强>
False None True and
as assert break class
continue def del elif
else except finally for
from global if import
in is lambda nonlocal
not or pass raise
return try while with
yield