ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Meta(type):
def __new__(m, name, bases, ns):
ns["kind"] = name.upper()
return super().__new__(m, name, bases, ns)
class A(metaclass=Meta): pass
class B(A): pass
print(A.kind, B.kind, type(B).__name__, isinstance(A, type))- A. A A Meta True
- B. A B type True
- C. A B Meta True
- D. A A type False
Correct answer: C. A B Meta True
A metaclass is inherited by subclasses, so creating B also runs Meta.__new__ and sets B.kind = 'B'. Classes are instances of their metaclass, which is itself a subclass of type.
More Python Programming questions
- What is printed? python class S: slots = ("a",) class T(S): pass t = T() t.a = 1 t.b = 2 s = S() try: s.b = 2 except AttributeError…
- What is printed? python class Account: rate = 0.05 def init(self, bal): self.bal = bal def interest(self): return self.bal self.rate a =…
- What is printed? python def countergen(): c = 0 while True: cmd = yield c if cmd == "reset": c = 0 else: c += 1 g = countergen()…
- What is printed? python import itertools c = itertools.count() data = ["a", "b", "c"] pairs = list(zip(c, data)) print(pairs, next(c)…
- What is printed? (Output lines are shown separated by " / ".) python def f(x): return x 2 g = f del f print(g(3), g.name) try: f(1) except…
- What is printed? python x = [1, 2, 3] it = iter(x) next(it) x.insert(0, 0) print(next(it), list(it))