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Можеш да извадиш стринговете в константи на класа. Така няма да се налага да ги дефинираш три пъти. Освен това, всичко изглежда ок.
|
f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | 1 | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
2 | class LogicMixin: | 2 | class LogicMixin: | ||
n | n | 3 | 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: | ||
n | 5 | return "Burn her!" | n | 8 | 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: | ||
n | 7 | return "Burn her!" | n | 10 | 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)): | ||
t | 9 | return "Burn her!" | t | 12 | 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 LogicMixin | 15 | return LogicMixin |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|