Graceful Missing Attributes with __getattr__

Hard ⏱ 18 min 29% acceptance ★★★★★ 4.7
Build Config wrapping a dict: normal attribute access (cfg.host) should look keys up in the wrapped dict via __getattr__ (which fires only when normal lookup fails), returning None for unknown keys instead of raising. Store the dict with self._data — and mind the recursion trap if you forget the underscore.

Examples

Example 1
Input
cfg = Config({'host': 'db1'}); cfg.host
Output
'db1'
Explanation

__getattr__ consulted the wrapped dict.

Example 2
Input
cfg.missing
Output
None
Explanation

Unknown keys return None, no AttributeError.

Constraints

  • Use __getattr__, not __getattribute__.
  • Unknown keys → None.

Topics

OOP__getattr__

Companies

MetaPalantirDatabricks

Hints

Hint 1

__getattr__ only fires for names not found normally.

Hint 2

self._data.get(name) completes it.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.