1# test __getattr__ 2 3class A: 4 def __init__(self, d): 5 self.d = d 6 7 def __getattr__(self, attr): 8 return self.d[attr] 9 10a = A({'a':1, 'b':2}) 11print(a.a, a.b) 12 13# test that any exception raised in __getattr__ propagates out 14class A: 15 def __getattr__(self, attr): 16 if attr == "value": 17 raise ValueError(123) 18 else: 19 raise AttributeError(456) 20a = A() 21try: 22 a.value 23except ValueError as er: 24 print(er) 25try: 26 a.attr 27except AttributeError as er: 28 print(er) 29