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

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

0 точки общо

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

 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, name):
 7
 8        if not name.startswith('spooky_') and not name.startswith('__'):
 9            return "Booooo, only ghosts here!"
10
11        raise AttributeError(f"{name} attribute does not exist.")
12
13    def __setattr__(self, name, value):
14
15        if not name.startswith('spooky_') and not name.startswith('__'):
16            name = f'spooky_{name}'
17
18        super().__setattr__(name, 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 11, in __getattr__
raise AttributeError(f"{name} attribute does not exist.")
AttributeError: spooky_spooky_buttler attribute does not exist.

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Дискусия
История
Това решение има само една версия.