Skip to main content
🔒 Preview mode. The first 15 Foundations lessons are free; this one is Pro. Start a 7-day trial to unlock the editor, AI hints and the rest of the curriculum. Card required, cancel any time in Dashboard.Start 7-day trial →
← CoursesSenior Deep-DivesModule 1 · Concurrency & Async InternalsMetaclassespredict5 / 161
+100 XP
Task
📝 **Task:** Predict the exact output. \`Meta.__new__\` prints \`meta_new:<name>\` as each class is built. \`Base.__init_subclass__\` prints \`init_sub:<name>\` and appends to \`registry\`. Two questions: (1) Does \`init_sub\` fire when \`Base\` itself is created? (2) Inside \`Meta.__new__\` — does \`print\` run before or after \`super().__new__\` builds the class? 📋 Implement the function above. Tests run automatically. 💡 **Hint:** Re-read the theory if you get stuck.
Predict the output

Read the code carefully

class Meta(type):
    def __new__(mcs, name, bases, ns):
        print(f"meta_new:{name}")
        return super().__new__(mcs, name, bases, ns)


class Base(metaclass=Meta):
    registry = []
    def __init_subclass__(cls, **kwargs):
        print(f"init_sub:{cls.__name__}")
        Base.registry.append(cls.__name__)


class A(Base):
    pass


class B(Base):
    pass


print(Base.registry)

What will the program print? Write here:

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…