Instead of composition, you could consider using a Abstract Base Class, with three different subclasses. In python, duck typing is favoured over explicitly checking types.
Python - Multiple "private" implementations for a class
-
14-07-2023 - |
Question
I have a public class which the module exports, and 3 implementations of it.
At certain points in the program, the implementation used will be changed dynamically, something along the lines of:
class PublicClass(object):
_IMPLEMENTATION_TO_USE = _Imp1
def func1(self, arg1):
_IMPLEMENTATION_TO_USE.func1(arg1)
class _Imp1(PublicClass):
def func1(self, arg1): pass
class _Imp2(PublicClass):
def func1(self, arg1): pass
What's the best (Pythonic) way of achieving it?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow