Factory class with abstractmethod
<p>I've created a factory class called <code>FitFunction</code> that adds a whole bunch of stuff beyond what I've shown. The label method <code>pretty_string</code> is supposed to just return the string as written. When I run this file, it prints a string that is as useful as the <code>repr</code>. Does someone know how I would go about implementing this?</p>

<pre><code>#!/usr/bin/env python

from __future__ import print_function, absolute_import

import abc
import types
import numpy as np

class FitFunction(object):

    def __init__(self, python_function):

        assert isinstance(python_function, types.FunctionType)
        self._py_function = python_function

    @abc.abstractmethod
    def pretty_string():
        r"""
        Return some pretty string.
        """


class Gaussian(FitFunction):


    def __init__(self):

        def gaussian(x, mu, sigma, A):
            coeff = (_np.sqrt(2.0 * _np.pi) * sigma)**(-1.0)
            arg   = -.5 * (((x - mu) / sigma)**2.0)
            return A * coeff * _np.exp(arg)

        FitFunction.__init__(self, gaussian)

        @staticmethod
        def pretty_string():
            return "1D Gaussian"


if __name__ == "__main__":

    print("Gaussian.pretty_string: %s" % Gaussian().pretty_string() )
</code></pre>

<p>I subclass <code>FitFunction</code> to create <code>Gaussian</code> because I apply <code>Gaussian</code> to many different data sets with the same parameters so that I can compare the output.</p>

<p>For reference, this is what happens when I execute the file:</p>

<pre><code>me$ ./FitFunction_SO_test.py 
Gaussian.pretty_string: &lt;bound method Gaussian.pretty_string of &lt;__main__.Gaussian object at 0x1005e2f90&gt;&gt;
</code></pre>

<p>I'm looking for the following result:</p>

<pre><code>me$ ./FitFunction_SO_test.py 
Traceback (most recent call last):
  File "./FitFunction_SO_test.py", line 43, in &lt;module&gt;
    print("Gaussian.pretty_string: %s" % Gaussian().pretty_string())
TypeError: pretty_string() takes no arguments (1 given)
</code></pre>

<p>Use:</p>

<pre><code>print("Gaussian.pretty_string: %s" % Gaussian.pretty_string())
</code></pre>

<p>Or else you are printing the <code>repr</code> of the <em>method</em>, not the <em>result of calling the method</em>, which is the string you are looking for.</p>

python abstract-factory abc