Puzzle 12 Attention Seeker
Python Brain Teasers — by Miki Tebeka (21 / 40)
👈 Click the Button | TOC | Identity Crisis 👉
1: class Seeker:
2: def __getattribute__(self, name):
3: if name not in self.__dict__:
4: return '<not found>'
5: return self.__dict__[name]
6:
7:
8: s = Seeker()
9: print(s.id)
Guess the Output
IMPORTANT
Try to guess what the output is before moving to the next page.
This code will raise a RecursionError exception.
When you write s.id, Python does an attribute lookup (see puzzle 1, Puzzle 1, Ready Player One). Python defines several hooks to bypass the usual attribute lookup algorithm. The two main options are __getattr__ and __getattribute__.
Other Options
INFORMATION
There are several other ways to modify attribute access such as staticmethod, classmethod, properties, descriptors, and more.