0
0
Pythonprogramming~20 mins

Object initialization flow in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Object Initialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of __init__ and __post_init__ in dataclasses

What is the output of this Python code using dataclasses?

Python
from dataclasses import dataclass, field

@dataclass
class Person:
    name: str
    age: int = field(default=0)

    def __post_init__(self):
        print(f"Post init: {self.name} is {self.age} years old")

p = Person("Alice", 30)
print(f"Name: {p.name}, Age: {p.age}")
A
Name: Alice, Age: 30
Post init: Alice is 30 years old
B
Name: Alice, Age: 0
Post init: Alice is 30 years old
C
Post init: Alice is 0 years old
Name: Alice, Age: 30
D
Post init: Alice is 30 years old
Name: Alice, Age: 30
Attempts:
2 left
💡 Hint

Remember that __post_init__ runs automatically after __init__ in dataclasses.

Predict Output
intermediate
2:00remaining
Order of __new__ and __init__ methods

What will be printed when this code runs?

Python
class MyClass:
    def __new__(cls):
        print("Creating instance")
        return super().__new__(cls)

    def __init__(self):
        print("Initializing instance")

obj = MyClass()
ACreating instance
B
Creating instance
Initializing instance
C
Initializing instance
Creating instance
DInitializing instance
Attempts:
2 left
💡 Hint

Think about which method runs first when creating an object.

Predict Output
advanced
2:00remaining
Effect of returning a different object in __new__

What is the output of this code?

Python
class A:
    def __new__(cls):
        print("A.__new__ called")
        return super().__new__(cls)

    def __init__(self):
        print("A.__init__ called")

class B(A):
    def __new__(cls):
        print("B.__new__ called")
        return "Not an instance"

    def __init__(self):
        print("B.__init__ called")

obj = B()
print(type(obj))
A
B.__new__ called
<class 'str'>
B
B.__new__ called
B.__init__ called
<class '__main__.B'>
C
A.__new__ called
A.__init__ called
<class '__main__.B'>
D
B.__new__ called
A.__init__ called
<class '__main__.B'>
Attempts:
2 left
💡 Hint

What happens if __new__ returns something that is not an instance of the class?

🧠 Conceptual
advanced
2:00remaining
Understanding attribute initialization order

Consider this class:

class C:
    def __init__(self):
        self.x = 10
        self.setup()

    def setup(self):
        self.x = 20

class D(C):
    def setup(self):
        self.x = 30

obj = D()
print(obj.x)

What is the output?

A20
B10
C30
DAttributeError
Attempts:
2 left
💡 Hint

Which setup method runs when obj = D() is created?

🔧 Debug
expert
2:00remaining
Why does this object initialization fail?

What error does this code raise and why?

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

class F(E):
    def __init__(self):
        print("Initializing F")

obj = F(5)
ATypeError: F.__init__() takes 1 positional argument but 2 were given
BAttributeError: 'F' object has no attribute 'value'
CSyntaxError: missing colon in class definition
DNo error, prints 'Initializing F'
Attempts:
2 left
💡 Hint

Check the parameters expected by F.__init__ versus what is passed.