Passing subclasses to imported library
<p>I have library which returns collections of some semi-abstract objects.</p>

<pre><code>class Item1(object):
    pass
class Item2(object):
    pass
class Collection1(object):
    pass 

class Provider(object):
    def retrieve_collection(self):
        col = Collection1()
        col.add(Item1())
        col.add(Item2())
        return col
</code></pre>

<p>It just populates attributes of the objects, and these item/collection classes are intended to be subclassed in caller code.</p>

<p>So there would be some script</p>

<pre><code>from mylib import Provider, Item1
class MyItem(Item1):
    pass
provider = Provider()
col = provider.retrieve_collection()
</code></pre>

<p>And the question is, what is elegant and pythonic solution to pass MyItem (and any other subclasses) to Provider?</p>

<p>I can pass it as <code>Provider(item1=MyItem)</code>, store it as <code>self.item1</code>, and then instantiate it as <code>self.item1()</code> instead of <code>Item1()</code>, but this seems fugly. Plus I'll have horrible, long constructor calls all over client code.</p>

<p>Other option would be to override classes on module level, like <code>mylib.Item1 = MyItem</code>, but this can lead to many unexpected and hard to debug problems. Besides, there could be several different provider classes which use same item base classes, but which would require different subclasses from client.</p>

<p>Maybe some form of classes registry and factories instead of actual classes? So Item1() would try to figure which class to instantiate based on some context, but I'm not sure how that would work.</p>
