`id`是python中的关键字吗?

时间:2011-06-14 22:21:54

标签: python keyword

我的编辑器(TextMate)以另一种颜色显示id(当用作变量名称时),然后显示我常用的变量名称。它是关键字吗?我不想遮蔽任何关键词...

4 个答案:

答案 0 :(得分:54)

id不是Python中的关键字,但它是built-in function的名称。

关键字are

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

答案 1 :(得分:17)

你也可以从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.)

答案 2 :(得分:7)

这是一个内置功能:

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

答案 3 :(得分:7)

仅适用于reference purposes

检查某些内容是否是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']