ProgrammingVery hardPython · GATE DA · Advanced
What is printed?
What is printed?
class Meta(type):
def __new__(m, name, bases, d):
d['tag'] = name.lower()
return super().__new__(m, name, bases, d)
class A(metaclass=Meta):
pass
class B(A):
pass
print(A.tag, B.tag, type(B).__name__, isinstance(B(), A))- A. a a Meta True
- B. a b Meta True
- C. a b type True
- D. a b Meta False
Correct answer: B. a b Meta True
A metaclass is inherited: creating B calls Meta.__new__ again with name 'B', so B.tag is 'b' (not inherited from A), type(B) is Meta, and instances of B are still instances of A.
More Python Programming questions
- What is printed? python print(type(10 / 5))
- Which of these built-in Python types is immutable?
- What is printed? python s = "placement" print(s[2:5])
- What is printed? python print(len({1, 2, 2, 3, 3, 3}))
- Which keyword turns an ordinary Python function into a generator function?
- What is printed? python x = [1, 2, 3] x.append([4, 5]) print(len(x))