KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / Python

object.__class__

Bij een gewone instantie verwijst het attribuut naar haar klasseobject.

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

Syntax

cls = instance.__class__

Bij een gewone instantie verwijst het attribuut naar haar klasseobject.

object.__class__

Voor x uit Child geeft het Child terwijl isinstance ook Base herkent. Het veranderen van de klasse vereist een compatibele instantie-indeling, ongelijke slots kunnen TypeError veroorzaken.

De uitvoer is 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

Voorbeelden

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

Resultaat

Child True True

De uitvoer is Child True True.

Bronnen