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

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

0 точки общо

0 успешни теста
1 неуспешни теста
Код
Скрий всички коментари

 1class HauntedMansion:
 2    prefix = "spooky_"  # so as to use it multiple times
 3
 4    def __init__(self, **kwargs):
 5        for key, value in kwargs.items():
 6            setattr(self, self.prefix + str(key), value)
 7
 8    def __getattr__(self, name):
 9        # not defined nor does it begin with the prefix
10        return "Booooo, only ghosts here!"
11
12    def __setattr__(self, name, value):
13        if not name.startswith(self.prefix) and not name.startswith("__"):
14            object.__setattr__(self, self.prefix + name, value)
15        else:
16            object.__setattr__(self, name, value)

F
======================================================================
FAIL: 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...")
AssertionError: 'Booooo, only ghosts here!' != 'Hehe, I said butt...'
- Booooo, only ghosts here!
+ Hehe, I said butt...

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

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

f1class HauntedMansion:f1class HauntedMansion:
2    prefix = "spooky_"  # so as to use it multiple times2    prefix = "spooky_"  # so as to use it multiple times
33
4    def __init__(self, **kwargs):4    def __init__(self, **kwargs):
5        for key, value in kwargs.items():5        for key, value in kwargs.items():
6            setattr(self, self.prefix + str(key), value)6            setattr(self, self.prefix + str(key), value)
77
8    def __getattr__(self, name):8    def __getattr__(self, name):
9        # not defined nor does it begin with the prefix9        # not defined nor does it begin with the prefix
10        return "Booooo, only ghosts here!"10        return "Booooo, only ghosts here!"
1111
12    def __setattr__(self, name, value):12    def __setattr__(self, name, value):
13        if not name.startswith(self.prefix) and not name.startswith("__"):13        if not name.startswith(self.prefix) and not name.startswith("__"):
t14            object.__setattr__(self, self.__class__.prefix + name, value)t14            object.__setattr__(self, self.prefix + name, value)
15        else:15        else:
16            object.__setattr__(self, name, value)16            object.__setattr__(self, name, value)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class HauntedMansion:f1class HauntedMansion:
n2    prefix = "spooky_" #so as to use it multiple timesn2    prefix = "spooky_"  # so as to use it multiple times
3 
3    def __init__(self, **kwargs):4    def __init__(self, **kwargs):
4        for key, value in kwargs.items():5        for key, value in kwargs.items():
n5            setattr(self, self.prefix+str(key), value)n6            setattr(self, self.prefix + str(key), value)
67
7    def __getattr__(self, name):8    def __getattr__(self, name):
t8        return "Booooo, only ghosts here!" #not defined nor does it begin with the prefixt9        # not defined nor does it begin with the prefix
10        return "Booooo, only ghosts here!"
911
10    def __setattr__(self, name, value):12    def __setattr__(self, name, value):
11        if not name.startswith(self.prefix) and not name.startswith("__"):13        if not name.startswith(self.prefix) and not name.startswith("__"):
12            object.__setattr__(self, self.__class__.prefix + name, value)14            object.__setattr__(self, self.__class__.prefix + name, value)
13        else:15        else:
14            object.__setattr__(self, name, value)16            object.__setattr__(self, name, value)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op