ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
class Meta(type):
    def __new__(mcs, name, bases, ns):
        print("creating", name)
        return super().__new__(mcs, name, bases, ns)
class C(metaclass=Meta):
    pass
  1. A. Nothing is printed
  2. B. creating C
  3. C. creating Meta
  4. D. creating type

Correct answer: B. creating C

Specifying metaclass=Meta makes Meta.__new__ run while the class statement itself is being executed (at class-creation time), printing 'creating C' immediately, before any instance of C is ever made.

More Python Programming questions

Exams that ask this topic