CKTIS / Python
object.__class__
Das Attribut einer gewöhnlichen Instanz verweist auf ihr Klassenobjekt.
Version: Python 3.14; repository image, production runtime not attested
Syntax
cls = instance.__class__Das Attribut einer gewöhnlichen Instanz verweist auf ihr Klassenobjekt.
object.__class__
Bei x aus Child ergibt es Child, während isinstance auch Base anerkennt. Ein Klassenwechsel erfordert kompatible Instanzlayouts, unterschiedliche Slots können TypeError auslösen.
Die Ausgabe ist 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
Beispiele
class Base: pass
class Child(Base): pass
x = Child()
print(x.__class__.__name__, x.__class__ is type(x), isinstance(x, Base))
Ergebnis
Child True True
Die Ausgabe ist Child True True.