Questions / Quant development, Python correctness
A property can outrank instance storage
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
- 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.