1def logic_mixin_factory(mass, mass_attr_name, material, material_attr_name, float_method_name):
2 # This is the mixin class we will define inside the factory function.
3 class LogicMixin:
4 # This method will be added to any class that inherits from LogicMixin.
5 def is_a_witch(self):
6 # Check if the instance has an attribute with the same name as mass_attr_name
7 # and if its value is equal to the mass parameter.
8 if hasattr(self, mass_attr_name) and getattr(self, mass_attr_name) == mass:
9 return "Burn her!"
10
11 # Check if the instance has an attribute with the same name as material_attr_name
12 # and if its value is equal to the material parameter.
13 if hasattr(self, material_attr_name) and getattr(self, material_attr_name) == material:
14 return "Burn her!"
15
16 # Check if the instance has a method with the same name as float_method_name.
17 # We use callable() to check if the attribute is a method that can be called.
18 if callable(getattr(self, float_method_name, None)):
19 return "Burn her!"
20
21 # If none of the conditions are met, the instance is not a witch.
22 return "No, but it's a pity, cuz she looks like a witch!"
23
24 # Return the mixin class so it can be used to create other classes.
25 return LogicMixin
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK