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

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

1 точки общо

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

 1class HauntedMansion:
 2    """HauntedMansion class needs a spooky_ var prefix for access to its named attributes."""
 3    __BOO__ = "Booooo, only ghosts here!"
 4    __SPOOKY__ = "spooky_"
 5
 6    def __init__(self, **kwargs):
 7        for name, value in kwargs.items():
 8            setattr(self, name, value)
 9
10    def __getattr__(self, name):
11        if name.startswith(self.__SPOOKY__) and name in self.__dict__:
12            return self.__dict__.get(name)
13        return self.__BOO__
14
15    def __setattr__(self, name, value):
16        object.__setattr__(self, self.__SPOOKY__ + name, value)

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

OK

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

f1class HauntedMansion:f1class HauntedMansion:
2    """HauntedMansion class needs a spooky_ var prefix for access to its named attributes."""2    """HauntedMansion class needs a spooky_ var prefix for access to its named attributes."""
3    __BOO__ = "Booooo, only ghosts here!"3    __BOO__ = "Booooo, only ghosts here!"
4    __SPOOKY__ = "spooky_"4    __SPOOKY__ = "spooky_"
55
6    def __init__(self, **kwargs):6    def __init__(self, **kwargs):
7        for name, value in kwargs.items():7        for name, value in kwargs.items():
8            setattr(self, name, value)8            setattr(self, name, value)
t9    t9 
10    def __getattr__(self, name):10    def __getattr__(self, name):
11        if name.startswith(self.__SPOOKY__) and name in self.__dict__:11        if name.startswith(self.__SPOOKY__) and name in self.__dict__:
12            return self.__dict__.get(name)12            return self.__dict__.get(name)
13        return self.__BOO__13        return self.__BOO__
1414
15    def __setattr__(self, name, value):15    def __setattr__(self, name, value):
16        object.__setattr__(self, self.__SPOOKY__ + name, value)16        object.__setattr__(self, self.__SPOOKY__ + name, value)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1class HauntedMansion:f1class HauntedMansion:
2    """HauntedMansion class needs a spooky_ var prefix for access to its named attributes."""2    """HauntedMansion class needs a spooky_ var prefix for access to its named attributes."""
3    __BOO__ = "Booooo, only ghosts here!"3    __BOO__ = "Booooo, only ghosts here!"
4    __SPOOKY__ = "spooky_"4    __SPOOKY__ = "spooky_"
55
6    def __init__(self, **kwargs):6    def __init__(self, **kwargs):
7        for name, value in kwargs.items():7        for name, value in kwargs.items():
8            setattr(self, name, value)8            setattr(self, name, value)
9    9    
10    def __getattr__(self, name):10    def __getattr__(self, name):
n11        if name.startswith("spooky_") and name in self.__dict__:n11        if name.startswith(self.__SPOOKY__) and name in self.__dict__:
12            return self.__dict__.get(name)12            return self.__dict__.get(name)
n13        return self.__class__.__BOO__n13        return self.__BOO__
1414
15    def __setattr__(self, name, value):15    def __setattr__(self, name, value):
t16        object.__setattr__(self, self.__class__.__SPOOKY__ + name, value)t16        object.__setattr__(self, self.__SPOOKY__ + name, value)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op