Is `id` a keyword in python?

27,972

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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.)
Share:
27,972
Aufwind
Author by

Aufwind

Updated on July 31, 2021

Comments

  • Aufwind
    Aufwind almost 3 years

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

  • Aufwind
    Aufwind about 13 years
    Thanks for the quick reply. I assume using id as an attribute in a class is same as bad? myobject = myclass(); myobject.id = 123; Would this shade the built-in function, too?
  • Greg Hewgill
    Greg Hewgill about 13 years
    @Aufwind: Using id as a class attribute is not as bad, because in Python you always have to qualify it with something (this.id or foo.id), so it always follows a .. Your editor may not understand this distinction.
  • Aufwind
    Aufwind about 13 years
    I couldn't come up with a synonyme that is as short as id. Good to hear, that, as an attribute, it has no sideeffects. :-)
  • Aufwind
    Aufwind about 13 years
    Thanks, I simply forgot about (the powerful) docu in python. :-)
  • Nicholas Knight
    Nicholas Knight about 13 years
    I'm utterly guilty of using id even as a local variable sometimes. It's so easy not to care when it's so rarely needed...
  • Aufwind
    Aufwind about 13 years
    Can I use help(somecommand) everytime I am not sure, if somecommand could be a keyword or a python built-in function to be sure?
  • joaquin
    joaquin about 13 years
    @Aufwind yes, you can. For keywords you must, however, use a string for example for the if statement you must do help('if').
  • Animesh Kumar
    Animesh Kumar about 3 years
    @Aufwind: in case of models, pk is a good substitute for id