Skip to main content

Practice · 1 of 3

Who wins the lookup?

CHALLENGE
Difficulty: advanced+25 XP

Implement a data descriptor Guarded that stores its value in the instance dict under the mangled name _value. Also implement def probe(obj) that returns the string 'descriptor' if obj.value resolves through the descriptor's __get__ (i.e. the descriptor increments its own reads counter), or 'instance' otherwise. The test will create an instance, set BOTH obj.value = 99 (a plain instance attribute attempt) and the descriptor state, and check that reading obj.value still goes through the descriptor.

Back to lesson: Practice: Attribute Lookup Practice