How do you define a function during dynamic Python type creation
<p>(This is in Python 3 btw)</p>

<p>Okay so say we use <code>type()</code> as a class constructor:</p>

<p><code>X = type('X', (), {})</code></p>

<p>What I'm trying to find is how would <code>type()</code> accept a function as an argument and allow it to be callable? </p>

<p>I'm looking for an example of how this could be accomplished. Something along the lines of:</p>

<pre><code>&gt;&gt;&gt; X = type('X', (), {'name':'World', 'greet':print("Hello {0}!".format(name))}
&gt;&gt;&gt; X.greet()
Hello World!
</code></pre>

<p>You need to pass the function.  In your code, you call the function, and pass the result.</p>

<p>Try:</p>

<pre><code>def print_hello(self):
    print("Hello {0}!".format(self.name))

X = type('X', (), {'name':'World', 'greet': print_hello})
</code></pre>

python python-3.x dynamic-programming metaclass class-method