Подтвердить что ты не робот

Является ли `id` ключевым словом в python?

Мой редактор (TextMate) показывает id другим цветом (при использовании в качестве имени переменной), а затем мои обычные имена переменных. Это ключевое слово? Я не хочу затенять любое ключевое слово...

4b9b3361

Ответ 1

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

С другой стороны, встроенные функции, такие как id или type или str, могут быть затенены:

str = "hello"    # don't do this

Ответ 2

Вы также можете получить помощь от 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 the object 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 the object memory address.)

Ответ 3

Это встроенная функция:

id(...)
    id(object) -> integer

    Return the identity of an object.  This is guaranteed to be unique among
    simultaneously existing objects.  (Hint: it the object memory address.)

Ответ 4

Только для справочных целей:

Проверьте, что в 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']