문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top