1class HauntedMansion:
2 def __init__(self, **kwargs):
3 for key, value in kwargs.items():
4 setattr(self, key, value)
5
6 def __getattribute__(self, name):
7 PREFIX = "spooky_"
8 if name.startswith(PREFIX):
9 return object.__getattribute__(self, name.removeprefix(PREFIX))
10 elif name.startswith("__") and name.endswith("__"):
11 return object.__getattribute__(self, name)
12 else:
13 return "Booooo, only ghosts here!"
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 25, in test_haunted_mansion
self.assertEqual(haunted_mansion.spooky_buttler, "Booooo, only ghosts here!")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 9, in __getattribute__
return object.__getattribute__(self, name.removeprefix(PREFIX))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'HauntedMansion' object has no attribute 'buttler'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
f | 1 | class HauntedMansion: | f | 1 | class HauntedMansion: |
2 | def __init__(self, **kwargs): | 2 | def __init__(self, **kwargs): | ||
3 | for key, value in kwargs.items(): | 3 | for key, value in kwargs.items(): | ||
4 | setattr(self, key, value) | 4 | setattr(self, key, value) | ||
5 | 5 | ||||
6 | def __getattribute__(self, name): | 6 | def __getattribute__(self, name): | ||
7 | PREFIX = "spooky_" | 7 | PREFIX = "spooky_" | ||
t | 8 | if name.startswith(PREFIX) and not name.startswith("__"): | t | 8 | if name.startswith(PREFIX): |
9 | return object.__getattribute__(self, name.removeprefix(PREFIX)) | 9 | return object.__getattribute__(self, name.removeprefix(PREFIX)) | ||
10 | elif name.startswith("__") and name.endswith("__"): | 10 | elif name.startswith("__") and name.endswith("__"): | ||
11 | return object.__getattribute__(self, name) | 11 | return object.__getattribute__(self, name) | ||
12 | else: | 12 | else: | ||
13 | return "Booooo, only ghosts here!" | 13 | return "Booooo, only ghosts here!" |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|