在 python 中是‘ id’关键字吗?

我的编辑器(TextMate)以另一种颜色(当用作变量名时)显示 id,而不是我通常使用的变量名。是关键词吗?我不想隐藏任何关键词。

30992 次浏览

在 Python 中,id不是 关键词,而是 内置功能的名称。

关键词 :

and       del       from      not       while
as        elif      global    or        with
assert    else      if        pass      yield
break     except    import    print
class     exec      in        raise
continue  finally   is        return
def       for       lambda    try

关键字是无效的变量名。下面是语法错误:

if = 1

另一方面,像 idtypestr这样的内置函数可以被隐藏:

str = "hello"    # don't do this

这是一个内置的功能:

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

你也可以从 python 那里得到帮助:

>>> help(id)
Help on built-in function id in module __builtin__:


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

或者你也可以质疑 IPython

IPython 0.10.2   [on Py 2.6.6]
[C:/]|1> id??
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in function id>
Namespace:      Python builtin
Docstring [source file open failed]:
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.)

返回文章页面 参考用途:

检查在 Python 中某些东西是否是关键字:

>>> import keyword
>>> keyword.iskeyword('id')
False

检查 Python 中的所有关键字:

>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try',
'while', 'with', 'yield']