Предизвикателства > Haunted mansion > Решения > Решението на Йована Божилов

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

0 точки общо

0 успешни теста
1 неуспешни теста
Код (Предизвикателство 2)
Скрий всички коментари

 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)

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