Questions / Quant development, Python correctness

A property can outrank instance storage

Challenge, about 10 minutes. Question D072.

What integer is printed?

class Price:
    @property
    def x(self):
        return 7
    @x.setter
    def x(self, value):
        pass
p = Price()
p.__dict__["x"] = 100
print(p.x)

Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.

Show the answer and worked solution

Answer: 7 value

  1. The property getter controls x even though the instance dictionary has an x entry. It returns 7, so the stored 100 does not determine attribute access.

Interview shortcut. Data descriptors beat the instance __dict__.

In the app this has a follow-up where one thing about the setup changes. Try the follow-up.

More python correctness questions