'id' is a bad variable name in Python

id() is a fundamental built-in:

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

In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.


In PEP 8 - Style Guide for Python Code, the following guidance appears in the section Descriptive: Naming Styles :

  • single_trailing_underscore_ : used by convention to avoid conflicts with Python keyword, e.g.

    Tkinter.Toplevel(master, class_='ClassName')

So, to answer the question, an example that applies this guideline is:

id_ = 42

Including the trailing underscore in the variable name makes the intent clear (to those familiar with the guidance in PEP 8).

Tags:

Python