Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

object.__class__

On an ordinary instance the attribute references its class object.

Version: Python 3.14; repository image, production runtime not attested

Syntax

cls = instance.__class__

On an ordinary instance the attribute references its class object.

object.__class__

For x constructed by Child it gives Child, while isinstance also accepts Base. Reassigning the class is restricted by compatible instance layouts; incompatible slots can raise TypeError.

The program prints Child True True.

class A: __slots__=('x',)
class B: __slots__=('x','y')
a=A()
try: a.__class__=B
except TypeError: pass
else: raise AssertionError
assert type(a) is A

Examples

class Base: pass
class Child(Base): pass
x = Child()
print(x.__class__.__name__, x.__class__ is type(x), isinstance(x, Base))

Result

Child True True

The program prints Child True True.

Sources