Предизвикателства > 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 WitchLogicMixin:
 3        def __init__(self):
 4            self.mass = mass
 5            self.mass_attr_name = mass_attr_name
 6            self.material = material
 7            self.material_attr_name = material_attr_name
 8            self.float_method_name = float_method_name
 9
10        def is_a_witch(self):
11            check_mass = hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass
12            check_material = hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material
13            check_method = hasattr(self, float_method_name) and callable(getattr(self, float_method_name))
14
15            if check_mass or check_method or check_material:
16                return "Burn her!"
17            else:
18                return "No, but it's a pity, cuz she looks like a witch!"
19
20    return WitchLogicMixin

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

OK

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

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 WitchLogicMixin:2    class WitchLogicMixin:
3        def __init__(self):3        def __init__(self):
4            self.mass = mass4            self.mass = mass
5            self.mass_attr_name = mass_attr_name5            self.mass_attr_name = mass_attr_name
6            self.material = material6            self.material = material
7            self.material_attr_name = material_attr_name7            self.material_attr_name = material_attr_name
8            self.float_method_name = float_method_name8            self.float_method_name = float_method_name
99
10        def is_a_witch(self):10        def is_a_witch(self):
11            check_mass = hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass11            check_mass = hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass
12            check_material = hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material12            check_material = hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material
13            check_method = hasattr(self, float_method_name) and callable(getattr(self, float_method_name))13            check_method = hasattr(self, float_method_name) and callable(getattr(self, float_method_name))
1414
15            if check_mass or check_method or check_material:15            if check_mass or check_method or check_material:
16                return "Burn her!"16                return "Burn her!"
17            else:17            else:
18                return "No, but it's a pity, cuz she looks like a witch!"18                return "No, but it's a pity, cuz she looks like a witch!"
1919
20    return WitchLogicMixin20    return WitchLogicMixin
2121
t22 t
23LogicMixin = logic_mixin_factory(20, 'mass', 'woo', 'material', 'floater')
24 
25class Woman(LogicMixin):
26    def __init__(self):
27        self.mass = 10
28        self.material = 'wo'
29        self.floate = 0
30 
31    def floater(self):
32        pass
33 
34woman = Woman()
35 
36print(woman.is_a_witch())
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op