Get reference of function inside function for usage in prototype/creation function
<p>I have two nested functions: The outer creates a creation method / prototype, the inner will create a concrete example of that prototype:</p>

<pre><code>class Example:
    def __init__(self, str):
        self.str = str

def make_prototype(proto_name):
    def make_example(example_name):
        return Example(proto_name + ' ' + example_name)
    return make_example

proto = make_prototype('Prototype 1')
ex1 = proto('Example 1')
</code></pre>

<p>Now, I'd like to remember the used creation function in the <code>Example</code>. I did it the following way:</p>

<pre><code>class Example:

    def __init__(self, str, proto):
        self.str = str
        self.proto = proto

def make_prototype(proto_name):
    class make_example:
        def __call__(self, example_name):
            return Example(proto_name + ' ' + example_name, self)
    return make_example()

proto = make_prototype('Prototype 1')
ex1 = proto('Example 1')
ex2 = ex1.proto('Example 2')
</code></pre>

<p>I think that's a relatively elegant and understandable solution. BUT would there be a way to do it without the nested <code>class make_example</code>? Would there be a way to do it like in the first version and getting a reference to the function <code>make_example</code> directly inside <code>make_example</code>? Something like:</p>

<pre><code>class Example:
    def __init__(self, str, proto):
        self.str = str
        self.proto = proto

def make_prototype(proto_name):
    def make_example(example_name):
        return Example(proto_name + ' ' + example_name, REFERENCE TO THIS FUNC)
    return make_example

proto = make_prototype('Prototype 1')
ex1 = proto('Example 1')
ex2 = ex1.proto('Example 2')
</code></pre>

<p>You can use <code>__call__</code> class method. Your example would look like this:</p>

<pre><code>class Example:
    def __init__(self, str, proto):
        self.str = str
        self.proto = proto

class MakePrototype():
    def __init__(self, name):
        self.name = name

    def __call__(self, proto_name):
        return Example(proto_name, self)



proto = MakePrototype('Prototype 1')
ex1 = proto('Example 1')
ex2 = ex1.proto('Example 2')
</code></pre>

python