1

From the python document the id() function:

Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

This is clear, I understand it. It a python class instance is passed to id function, it returns the “identity” / address of that object. However, if a python class name is passed to id function. What does it return? From example.

class Abc:
    value = 0

x = Abc()

id(x)  # -> 65097560

id(Abc)  # -> 67738424

I understand 65097560 is the memory address of object x. but what is the meaning of id(Abc) / 67738424?

wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

1

Classes are objects themselves. They're instances of the metaclass type. (type is also used for determining something's type.)

>>> class Abc: pass
... 
>>> type(Abc)
<class 'type'>
>>> id(Abc)
39363368

In Python 2, old-style classes are instances of classobj.

>>> class Old: pass
... 
>>> type(Old)
<type 'classobj'>
>>> id(Old)
140369496387664
>>> class New(object): pass
... 
>>> type(New)
<type 'type'>
>>> id(New)
94676790299104

In general, everything is an object in Python, including functions like id() itself, builtin types/classes like list, and modules like sys. Though there are a few exceptions like keywords and operators.

wjandrea
  • 28,235
  • 9
  • 60
  • 81