Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

object.__init__

The initializer sets state on an already created instance and must return None.

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

Syntax

def __init__(self, value):
    self.value = value

The initializer sets state on an already created instance and must return None.

object.__init__

Normal construction invokes it after __new__ when the result is an instance of the requested class. An overriding initializer must call super().__init__(...) if base initialization is needed. Calling super() alone creates a proxy.

Child sets Ada and 4. Returning 7 from the Bad initializer raises TypeError.

calls=[]
class Other:
    def __new__(cls): return 'already made'
    def __init__(self): calls.append(1)
assert Other()=='already made' and not calls

Examples

class Base:
    def __init__(self, name): self.name = name
class Child(Base):
    def __init__(self, name, age):
        super().__init__(name)
        self.age = age
x = Child("Ada", 4)
print(x.name, x.age)
class Bad:
    def __init__(self): return 7
try: Bad()
except TypeError as exc: print(type(exc).__name__)

Result

Ada 4
TypeError

Child sets Ada and 4. Returning 7 from the Bad initializer raises TypeError.

Sources