Is it possible to generate dynamically class methods which are very similar one another?
Say I have a class like this one:
class KitchenDrawer:
def _check_if_tool_available(self, tool) -> bool:
# check if "tool" is in the drawer
def is_knife_available(self) -> bool:
return self._check_if_tool_available("knife")
def is_spoon_available(self) -> bool:
return self._check_if_tool_available("spoon")
def is_fork_available(self) -> bool:
return self._check_if_tool_available("fork")
Is there a way to generate is_knife_available(), is_spoon_available(), is_fork_available() dynamically, so there is not so much repeated code?
(Some of you might suggest to simply make _check_if_tool_available(self, tool) a public facing method and carry on with my life. Fair. But more than the sensible solution, I am interested in the nitty gritty issue here.)
Thanks!
evalto create the functions, but that would just be a hacky way of emulating a parameter.eval), but it’s just better practice to not make use of Python’s dynamic features – static analysis (both by tools and humans) is so valuable. I would write out the list of repetitive methods, assuming they’re this simple in reality.