title: Which way is ideal for Python factory registration?
body: <p>This is a question about which of these methods would be considered as the most <em>Pythonic</em>. I'm not looking for personal opinions, but, instead, what is idiomatic. My background is not in Python, so this will help me.</p>
  
  <p>I'm working on a Python 3 project which is extensible. The idea is similar to the factory pattern, except it is based on functions.</p>
  
  <p>Essentially, users will be able to create a custom function (across packages and projects) which my tool can locate and dynamically invoke. It will also be able to use currying to pass arguments down (but that code is not included here)</p>
  
  <p>My goal is for this to follow good-Pythonic practice. I'm torn between two strategies. And, since Python is not my expertise, I would like to know the pros/cons of the following practices:</p>
  
  <ol>
  <li><p><strong>Use a decorator</strong></p>
  
  <pre><code>registered = {}
  
  def factoried(name):
      def __inner_factory_function(fn):
          registered[name] = fn
          return fn
      return __inner_factory_function
  
  
  def get_function(name):
      return registered[name]
  </code></pre>
  
  <p>Then, the following function is automatically registered...</p>
  
  <pre><code>@factoried('foo')
  def build_foo():
    print('hi')
  </code></pre>
  
  <p>This seems reasonable, but does appear slightly magical to those who are not familiar with decorators.</p></li>
  <li><p><strong>Force sub-classing of an abstract class and use <code>__subclasses__()</code></strong></p>
  
  <p>If subclasses are used, there's no need for registration. However, I feel like this forces classes to be defined when a full class may be unnecessary. Also, the use of <code>.__subclasses__()</code> under the hood could seem magical to consumers as well. However, even Java can be used to search for classes with annotations.</p></li>
  <li><p><strong>Explicit registration</strong></p>
  
  <p>Forget all of the above and force explicit registration. No decorators. No subclasses. Just something like this:</p>
  
  <pre><code>def build_foo():
    # ...
  
  factory.register('foo', build_foo)
  </code></pre></li>
  </ol>