1class HauntedMansion:
2 def __init__(self, **kwargs):
3 for key, value in kwargs.items():
4 setattr(self, f'spooky_{key}', value)
5
6 def __getattr__(self, item):
7 if item.startswith('spooky_'):
8 return object.__getattribute__(self, item)
9 else:
10 return "Booooo, only ghosts here!"
11
12 def __setattr__(self, key, value):
13 if key.startswith('spooky_'):
14 super().__setattr__(key, value)
15 else:
16 super().__setattr__(f'spooky_{key}', value)
E
======================================================================
ERROR: test_haunted_mansion (test.TestHauntedMansion.test_haunted_mansion)
The mansion should be really spooky and haunted.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 22, in test_haunted_mansion
self.assertEqual(haunted_mansion.spooky_spooky_buttler, "Hehe, I said butt...")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 8, in __getattr__
return object.__getattribute__(self, item)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'HauntedMansion' object has no attribute 'spooky_spooky_buttler'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
02.11.2024 21:31