f | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): | f | def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name): |
| class WitchLogicMixin: | | class WitchLogicMixin: |
| def __init__(self): | | def __init__(self): |
| self.mass = mass | | self.mass = mass |
| self.mass_attr_name = mass_attr_name | | self.mass_attr_name = mass_attr_name |
| self.material = material | | self.material = material |
| self.material_attr_name = material_attr_name | | self.material_attr_name = material_attr_name |
| self.float_method_name = float_method_name | | self.float_method_name = float_method_name |
| | | |
| def is_a_witch(self): | | def is_a_witch(self): |
| check_mass = hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass | | check_mass = hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass |
| check_material = hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material | | check_material = hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material |
| check_method = hasattr(self, float_method_name) and callable(getattr(self, float_method_name)) | | check_method = hasattr(self, float_method_name) and callable(getattr(self, float_method_name)) |
| | | |
| if check_mass or check_method or check_material: | | if check_mass or check_method or check_material: |
| return "Burn her!" | | return "Burn her!" |
| else: | | else: |
| return "No, but it's a pity, cuz she looks like a witch!" | | return "No, but it's a pity, cuz she looks like a witch!" |
| | | |
| return WitchLogicMixin | | return WitchLogicMixin |
| | | |
t | | t | |
| LogicMixin = logic_mixin_factory(20, 'mass', 'woo', 'material', 'floater') | | |
| | | |
| class Woman(LogicMixin): | | |
| def __init__(self): | | |
| self.mass = 10 | | |
| self.material = 'wo' | | |
| self.floate = 0 | | |
| | | |
| def floater(self): | | |
| pass | | |
| | | |
| woman = Woman() | | |
| | | |
| print(woman.is_a_witch()) | | |