Предизвикателства > She's a witch! > Решения > Решението на Георги Кунчев

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

1 точки общо

1 успешни теста
0 неуспешни теста
Код (Fix typo)

 1def logic_mixin_factory(duck_mass: int,
 2                        mass_attr_name: str,
 3                        material: str,
 4                        material_attr_name: str,
 5                        float_method_name: str):
 6    class LogicMixin:
 7
 8        def __has_mass_of_a_duck(self):
 9            try:
10                return getattr(self, mass_attr_name) == duck_mass
11            except AttributeError:
12                return False
13
14        def __can_float(self):
15            try:
16                float_method = getattr(self, float_method_name)
17                return '__call__' in dir(float_method)
18            except AttributeError:
19                return False
20        
21        def __is_made_of_wood(self):
22            try:
23                return getattr(self, material_attr_name) == material
24            except AttributeError:
25                return False
26
27        def is_a_witch(self):
28            if self.__has_mass_of_a_duck() or self.__can_float() or self.__is_made_of_wood():
29                return "Burn her!"
30            return "No, but it's a pity, cuz she looks like a witch!"
31
32    return LogicMixin

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

OK

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

f1def logic_mixin_factory(duck_mass: int,f1def logic_mixin_factory(duck_mass: int,
2                        mass_attr_name: str,2                        mass_attr_name: str,
3                        material: str,3                        material: str,
4                        material_attr_name: str,4                        material_attr_name: str,
5                        float_method_name: str):5                        float_method_name: str):
6    class LogicMixin:6    class LogicMixin:
77
8        def __has_mass_of_a_duck(self):8        def __has_mass_of_a_duck(self):
9            try:9            try:
10                return getattr(self, mass_attr_name) == duck_mass10                return getattr(self, mass_attr_name) == duck_mass
11            except AttributeError:11            except AttributeError:
12                return False12                return False
1313
14        def __can_float(self):14        def __can_float(self):
15            try:15            try:
16                float_method = getattr(self, float_method_name)16                float_method = getattr(self, float_method_name)
17                return '__call__' in dir(float_method)17                return '__call__' in dir(float_method)
18            except AttributeError:18            except AttributeError:
19                return False19                return False
20        20        
21        def __is_made_of_wood(self):21        def __is_made_of_wood(self):
22            try:22            try:
23                return getattr(self, material_attr_name) == material23                return getattr(self, material_attr_name) == material
24            except AttributeError:24            except AttributeError:
25                return False25                return False
2626
27        def is_a_witch(self):27        def is_a_witch(self):
28            if self.__has_mass_of_a_duck() or self.__can_float() or self.__is_made_of_wood():28            if self.__has_mass_of_a_duck() or self.__can_float() or self.__is_made_of_wood():
29                return "Burn her!"29                return "Burn her!"
t30            return "No, but it's a pitty, cuz she looks like a witch!"t30            return "No, but it's a pity, cuz she looks like a witch!"
3131
32    return LogicMixin32    return LogicMixin
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op