CKTIS / Python
object.__class__
Pri bežnej inštancii atribút odkazuje na objekt jej triedy.
Verzia: Python 3.14; repository image, production runtime not attested
Syntax
cls = instance.__class__Pri bežnej inštancii atribút odkazuje na objekt jej triedy.
object.__class__
Pre x vytvorené cez Child dá Child, zatiaľ čo isinstance rozpozná aj Base. Zmena triedy vyžaduje kompatibilné rozloženie inštancií, nezlučiteľné sloty môžu vyvolať TypeError.
Výstup je 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
Príklady
class Base: pass
class Child(Base): pass
x = Child()
print(x.__class__.__name__, x.__class__ is type(x), isinstance(x, Base))
Výsledok
Child True True
Výstup je Child True True.