Factory functions and subclasses
<p>I have the following class hierarchy:</p>

<pre><code>class A(object):
    def __init__(self, filename, x):
        self.x = x
        # initialize A from filename

    # rest of A's methods

class B(A):
    def __init__(self, filename):
        super(B, self).__init__(filename, 10)

    # rest of B's methods
</code></pre>

<p>Both classes are passed the name of a file in their <code>__init__</code> methods. The file contents are then used to initialize some data in the objects.</p>

<p>To facilitate testing I'd like to be able to construct an <code>A</code> or <code>B</code> instance by passing it the data directly, rather than having the object read from a file. I thought that a pair of factory functions, each implemented using a <code>classmethod</code> might work, but I've been unable to get the versions in <code>B</code> to work at all. Here's what I have so far:</p>

<pre><code>class A(object):
    def __init__(self, x):
        self.x = x

    @classmethod
    def construct(cls, filename, x):
        a = cls(x)
        # initialize a from filename
        return a

    @classmethod
    def test_construct(cls, data, x):
        a = cls(x)
        # initialize a from data
        return a

class B(A):
    def __init__(self):
        super(B, self).__init__(10)

    @classmethod
    def construct(cls, filename):
        # should construct B from filename

    @classmethod
    def test_construct(cls, data):
        # should construct B from data
</code></pre>
