ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
def deco(cls):
    orig = cls.__init__
    def __init__(self, *a):
        orig(self, *a)
        self.ok = True
    cls.__init__ = __init__
    return cls
@deco
class P:
    def __init__(self, x):
        self.x = x
p = P(4)
print(p.x, p.ok, P.__name__)
  1. A. 4 AttributeError P
  2. B. 4 True deco
  3. C. 4 True P
  4. D. None True P

Correct answer: C. 4 True P

A class decorator can patch methods: the new __init__ calls the saved original and then adds an attribute. The decorator returns the same class object, so its name is still P.

More Python Programming questions

Exams that ask this topic