title: How to implement a strategy pattern with runtime selection of a method?
body: <p><strong><em>Context</em></strong></p>
  
  <p>I'm trying to implement some variant of strategy pattern in Python 2.7.
  I want to be able to instantiate a 'my_strategy' base class, but switch between different implementations of a 'score' method at run-time.
  I will have many common methods in 'my_strategy' but a bunch of 'score' implementations.
  The <strong>main</strong> illustrates how I want to use it.
  Here the scoring implementation is dummy of course.</p>
  
  <p><strong><em>What I tried (i.e. My code so far)</em></strong></p>
  
  <p>strategy.py:</p>
  
  <pre><code>from algo_one import *
  #from algo_two import *
  
  
  class my_strategy ( object ):
  
      def __init__(self, candidate = ""):
          self.candidate = candidate
          self.method = 'default'
          self.no = 10
          self._algo = algo_one
  
      def set_strategy(self, strategy='default'):
          self.strategy = strategy
          if self.strategy == 'algo_one':
              self._algo = algo_one
          elif self.strategy == 'algo_two':
              # self._algo = algo_two
              pass 
          else:
              self._algo = None
  
      def score(self, *args):
          if len(args) &gt; 0:
              self.candidate = args[0]
          self._algo.score(self.candidate)
  
  if  __name__ == "__main__":
      s = my_strategy()  
      s.strategy = 'algo_one'
      s.candidate = "hello world"
      print s.score()  
      print s.score("hi")
      # s.set_method('algo_two')
      # print s.score("hi")
  </code></pre>
  
  <p>I want to save the selected strategy in some sort of private pointer to the sub-class method.</p>
  
  <p>algo_one.py:</p>
  
  <pre><code>from strategy import my_strategy
  class algo_one ( my_strategy ):    
      def score(self, candidate):
          return len(candidate)*self.no 
  </code></pre>
  
  <p>I could have a class-less method, but later I'll need to access public variables of the base class. </p>
  
  <p>algo_two.py:</p>
  
  <pre><code>from strategy import my_strategy
  class algo_two ( my_strategy ):
      def score(self, candidate):
          return len(candidate)*3 
  </code></pre>
  
  <p>I have an empty <strong>init</strong>.py too.</p>
  
  <p><strong><em>The errors</em></strong></p>
  
  <p><strong>1.</strong>
  in score self._algo.score(self.candidate)</p>
  
  <blockquote>
    <p>TypeError: unbound method score() must be called with algo_one
    instance as first argument (got str instance instead)</p>
  </blockquote>
  
  <p><strong>2.</strong>
  If I uncomment the import of the second strategy:</p>
  
  <pre><code>from algo_two import *
  </code></pre>
  
  <p>I get the following error.  </p>
  
  <blockquote>
    <p>ImportError: cannot import name my_strategy</p>
  </blockquote>
  
  <p>My guess is that I run into some sort of circular dependency.  </p>
  
  <p><strong>3.</strong></p>
  
  <pre><code>from algo_one import *
  </code></pre>
  
  <p>This is obviously not pretty (unable to detect undefined names), but if I </p>
  
  <pre><code>from algo_one import algo_one
  </code></pre>
  
  <p>I get</p>
  
  <blockquote>
    <p>ImportError: cannot import name algo_one</p>
  </blockquote>
  
  <p><strong><em>Question</em></strong></p>
  
  <p>I think the errors are intertwined and that my approach, as a whole, may be flawed. If not just addressing the error, I'm looking for suggestions to improve the design. Or any comment, really. Also I'm open to suggestions regarding the title of this question. Thank you!</p>