id 是 python 中的关键字吗?

发布于 2024-11-15 14:14:34 字数 86 浏览 0 评论 0原文

我的编辑器 (TextMate) 以与我常用的变量名称不同的颜色(用作变量名称时)显示 id。是关键字吗?我不想遮蔽任何关键字...

My editor (TextMate) shows id in another colour (when used as variable name) than my usual variable names. Is it a keyword? I don't want to shade any keyword...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉宸 2024-11-22 14:14:35

这是一个内置函数:

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

It's a built in function:

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.)
带上头具痛哭 2024-11-22 14:14:34

id 不是 Python 中的关键字,但它是 内置函数

关键字

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 is not a keyword in Python, but it is the name of a built-in function.

The keywords 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

Keywords are invalid variable names. The following would be a syntax error:

if = 1

On the other hand, built-in functions like id or type or str can be shadowed:

str = "hello"    # don't do this
软甜啾 2024-11-22 14:14:34

您还可以从 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.)

You can also get help from 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.)

or alternatively you can question 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.)
静待花开 2024-11-22 14:14:34

仅供参考:

检查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']

Just for reference purposes:

Check if something is a keyword in Python:

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

Check all the keywords in 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']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文