Предизвикателства > She's a witch! > Решения > Решението на Виктор Райков

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

1 точки общо

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

 1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
 2    class LogicMixin:
 3        WITCH_MESSAGE = "Burn her!"
 4        NOT_WITCH_MESSAGE = "No, but it's a pity, cuz she looks like a witch!"
 5        
 6        def is_a_witch(self):
 7            if hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass:
 8                return LogicMixin.WITCH_MESSAGE
 9            if hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material:
10                return LogicMixin.WITCH_MESSAGE
11            if hasattr(self, float_method_name) and callable(getattr(self, float_method_name)):
12                return LogicMixin.WITCH_MESSAGE
13            return LogicMixin.NOT_WITCH_MESSAGE
14    
15    return LogicMixin

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

OK

Дискусия
Георги Кунчев
08.11.2023 15:49

Можеш да извадиш стринговете в константи на класа. Така няма да се налага да ги дефинираш три пъти. Освен това, всичко изглежда ок.
История

f1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):f1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
2    class LogicMixin:2    class LogicMixin:
nn3        WITCH_MESSAGE = "Burn her!"
4        NOT_WITCH_MESSAGE = "No, but it's a pity, cuz she looks like a witch!"
5        
3        def is_a_witch(self):6        def is_a_witch(self):
4            if hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass:7            if hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass:
n5                return "Burn her!"n8                return LogicMixin.WITCH_MESSAGE
6            if hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material:9            if hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material:
n7                return "Burn her!"n10                return LogicMixin.WITCH_MESSAGE
8            if hasattr(self, float_method_name) and callable(getattr(self, float_method_name)):11            if hasattr(self, float_method_name) and callable(getattr(self, float_method_name)):
t9                return "Burn her!"t12                return LogicMixin.WITCH_MESSAGE
10            return "No, but it's a pity, cuz she looks like a witch!"13            return LogicMixin.NOT_WITCH_MESSAGE
11    14    
12    return LogicMixin15    return LogicMixin
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op