CKTIS / Python
object.__class__
Atrybut zwykłej instancji wskazuje obiekt klasy, do której ta instancja należy.
Wersja: Python 3.14; repository image, production runtime not attested
Składnia
cls = instance.__class__Atrybut zwykłej instancji wskazuje obiekt klasy, do której ta instancja należy.
object.__class__
Dla x utworzonego przez Child daje Child, podczas gdy isinstance uznaje także Base. Przypisanie nowej klasy jest ograniczone zgodnością układu obiektów; różne slots mogą spowodować TypeError.
Program wypisuje 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
Przykłady
class Base: pass
class Child(Base): pass
x = Child()
print(x.__class__.__name__, x.__class__ is type(x), isinstance(x, Base))
Wynik
Child True True
Program wypisuje Child True True.