Is `id` a keyword in python?

Aufwind picture Aufwind · Jun 15, 2011 · Viewed 24k times · Source

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

Answer

Greg Hewgill picture Greg Hewgill · Jun 15, 2011

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