Предизвикателства > Haunted mansion > Решения > Решението на Марина Господинова

Резултати
0 точки от тестове
0 точки от учител

0 точки общо

0 успешни теста
1 неуспешни теста
Код

 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)

Дискусия
История

f1class HauntedMansion:f1class 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)
55
6    def __getattribute__(self, name):6    def __getattribute__(self, name):
7        PREFIX = "spooky_"7        PREFIX = "spooky_"
t8        if name.startswith(PREFIX) and not name.startswith("__"):t8        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
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op