Why is it bad to name a variable id
in Python?
为什么在Python中命名变量id是不好的?
8 个解决方案
#1
105
id()
is a fundamental built-in:
id()是一个基本的内置:
Help on built-in function
id
in module__builtin__
:有关模块__builtin__中内置函数id的帮助:
id(...) id(object) -> integer Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.)
In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.
通常,使用以任何语言删除关键字或内置函数的变量名称都是一个坏主意,即使它是允许的。
#2
44
id
is a built-in function that gives the memory address of an object. If you name one of your functions id
, you will have to say __builtins__.id
to get the original. Renaming id
globally is confusing in anything but a small script.
id是一个内置函数,它给出了一个对象的内存地址。如果您将其中一个函数命名为id,则必须说__builtins__.id才能获得原始函数。除了小脚本之外,全局重命名id令人困惑。
However, reusing built-in names as variables isn't all that bad as long as the use is local. Python has a lot of built-in functions that (1) have common names and (2) you will not use much anyway. Using these as local variables or as members of an object is OK because it's obvious from context what you're doing:
但是,只要使用本地名称,重复使用内置名称作为变量并不是那么糟糕。 Python有许多内置函数,(1)具有通用名称,(2)无论如何都不会使用太多。使用这些作为局部变量或作为对象的成员是可以的,因为从上下文中可以明显看出您正在做什么:
Example:
例:
def numbered(filename):
file = open(filename)
for i,input in enumerate(file):
print "%s:\t%s" % (i,input)
file.close()
Some built-ins with tempting names:
一些具有诱人名字的内置插件:
id
- ID
file
- 文件
list
- 名单
map
- 地图
-
all
,any
- 所有,任何
complex
- 复杂
dir
- DIR
input
- 输入
slice
- 片
buffer
- 缓冲
#3
36
I might say something unpopular here: id()
is a rather specialized built-in function that is rarely used in business logic. Therefore I don't see a problem in using it as a variable name in a tight and well-written function, where it's clear that id doesn't mean the built-in function.
我可能会说一些不受欢迎的东西:id()是一个相当专业的内置函数,在业务逻辑中很少使用。因此,我没有看到在一个紧凑且编写良好的函数中将它用作变量名的问题,其中很明显id并不意味着内置函数。
#4
28
In PEP 8 - Style Guide for Python Code, the following guidance appears in the section Descriptive: Naming Styles :
在PEP 8 - Python代码样式指南中,以下指南出现在描述性:命名样式部分中:
single_trailing_underscore_
: used by convention to avoid conflicts with Python keyword, e.g.single_trailing_underscore_:由约定用于避免与Python关键字冲突,例如
Tkinter.Toplevel(master, class_='ClassName')
Tkinter.Toplevel(master,class _ ='ClassName')
So, to answer the question, an example that applies this guideline is:
因此,要回答这个问题,适用本指南的一个例子是:
id_ = 42
Including the trailing underscore in the variable name makes the intent clear (to those familiar with the guidance in PEP 8).
在变量名中包含尾随下划线使得意图清晰(对于那些熟悉PEP 8中的指导的人)。
#5
3
It's bad to name any variable after a built in function. One of the reasons is because it can be confusing to a reader that doesn't know the name is overridden.
在内置函数之后命名任何变量是不好的。其中一个原因是因为对于不知道名称被覆盖的读者来说可能会造成混淆。
#6
1
Because it's the name of a builtin function.
因为它是内置函数的名称。
#7
1
'id' is a built-in method in Python. Assigning a value to 'id' will overwrite the method. It is best to use either an identifier before as in "some_id" or use it in a different capitalization method.
'id'是Python中的内置方法。将值分配给“id”将覆盖该方法。最好在“some_id”之前使用标识符,或者在不同的大小写方法中使用它。
The built in method takes a single parameter and returns an integer for the memory address of the object that you passed.
内置方法接受一个参数,并为您传递的对象的内存地址返回一个整数。
>>>
id(1)
>>> ID(1)
9787760
9787760
>>>
x = 1
>>> x = 1
>>>
id(x)
>>> ID(x)的
9787760
9787760
#8
-5
Because python is a dynamic language, it's not usually a good idea to give a variable and a function the same name. id() is a function in python, so it's recommend not to use a variable named id. Bearing that in mind, that applies to all functions that you might use... a variable shouldn't have the same name as a function.
因为python是一种动态语言,所以给变量和函数赋予相同的名称通常不是一个好主意。 id()是python中的一个函数,因此建议不要使用名为id的变量。请记住,这适用于您可能使用的所有函数......变量不应与函数同名。
#1
105
id()
is a fundamental built-in:
id()是一个基本的内置:
Help on built-in function
id
in module__builtin__
:有关模块__builtin__中内置函数id的帮助:
id(...) id(object) -> integer Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.)
In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.
通常,使用以任何语言删除关键字或内置函数的变量名称都是一个坏主意,即使它是允许的。
#2
44
id
is a built-in function that gives the memory address of an object. If you name one of your functions id
, you will have to say __builtins__.id
to get the original. Renaming id
globally is confusing in anything but a small script.
id是一个内置函数,它给出了一个对象的内存地址。如果您将其中一个函数命名为id,则必须说__builtins__.id才能获得原始函数。除了小脚本之外,全局重命名id令人困惑。
However, reusing built-in names as variables isn't all that bad as long as the use is local. Python has a lot of built-in functions that (1) have common names and (2) you will not use much anyway. Using these as local variables or as members of an object is OK because it's obvious from context what you're doing:
但是,只要使用本地名称,重复使用内置名称作为变量并不是那么糟糕。 Python有许多内置函数,(1)具有通用名称,(2)无论如何都不会使用太多。使用这些作为局部变量或作为对象的成员是可以的,因为从上下文中可以明显看出您正在做什么:
Example:
例:
def numbered(filename):
file = open(filename)
for i,input in enumerate(file):
print "%s:\t%s" % (i,input)
file.close()
Some built-ins with tempting names:
一些具有诱人名字的内置插件:
id
- ID
file
- 文件
list
- 名单
map
- 地图
-
all
,any
- 所有,任何
complex
- 复杂
dir
- DIR
input
- 输入
slice
- 片
buffer
- 缓冲
#3
36
I might say something unpopular here: id()
is a rather specialized built-in function that is rarely used in business logic. Therefore I don't see a problem in using it as a variable name in a tight and well-written function, where it's clear that id doesn't mean the built-in function.
我可能会说一些不受欢迎的东西:id()是一个相当专业的内置函数,在业务逻辑中很少使用。因此,我没有看到在一个紧凑且编写良好的函数中将它用作变量名的问题,其中很明显id并不意味着内置函数。
#4
28
In PEP 8 - Style Guide for Python Code, the following guidance appears in the section Descriptive: Naming Styles :
在PEP 8 - Python代码样式指南中,以下指南出现在描述性:命名样式部分中:
single_trailing_underscore_
: used by convention to avoid conflicts with Python keyword, e.g.single_trailing_underscore_:由约定用于避免与Python关键字冲突,例如
Tkinter.Toplevel(master, class_='ClassName')
Tkinter.Toplevel(master,class _ ='ClassName')
So, to answer the question, an example that applies this guideline is:
因此,要回答这个问题,适用本指南的一个例子是:
id_ = 42
Including the trailing underscore in the variable name makes the intent clear (to those familiar with the guidance in PEP 8).
在变量名中包含尾随下划线使得意图清晰(对于那些熟悉PEP 8中的指导的人)。
#5
3
It's bad to name any variable after a built in function. One of the reasons is because it can be confusing to a reader that doesn't know the name is overridden.
在内置函数之后命名任何变量是不好的。其中一个原因是因为对于不知道名称被覆盖的读者来说可能会造成混淆。
#6
1
Because it's the name of a builtin function.
因为它是内置函数的名称。
#7
1
'id' is a built-in method in Python. Assigning a value to 'id' will overwrite the method. It is best to use either an identifier before as in "some_id" or use it in a different capitalization method.
'id'是Python中的内置方法。将值分配给“id”将覆盖该方法。最好在“some_id”之前使用标识符,或者在不同的大小写方法中使用它。
The built in method takes a single parameter and returns an integer for the memory address of the object that you passed.
内置方法接受一个参数,并为您传递的对象的内存地址返回一个整数。
>>>
id(1)
>>> ID(1)
9787760
9787760
>>>
x = 1
>>> x = 1
>>>
id(x)
>>> ID(x)的
9787760
9787760
#8
-5
Because python is a dynamic language, it's not usually a good idea to give a variable and a function the same name. id() is a function in python, so it's recommend not to use a variable named id. Bearing that in mind, that applies to all functions that you might use... a variable shouldn't have the same name as a function.
因为python是一种动态语言,所以给变量和函数赋予相同的名称通常不是一个好主意。 id()是python中的一个函数,因此建议不要使用名为id的变量。请记住,这适用于您可能使用的所有函数......变量不应与函数同名。