What is and isn't available to class methods in python?
<p>My limited understand is that the second argument to types.FunctionTypes (<code>dict</code>) determines what is available to the function code. I'm curious how it gets gets decided when instantiating functions internally. To illustrate what I mean, if I try to copy a function dynamically:</p>

<pre><code>a = 'test'
def foo():
    print(a)

bar = types.FunctionType(
    foo.__code__, 
    globals(), 
    'bar', 
    foo.__defaults__, 
    foo.__closure__
)
</code></pre>

<p>This seems to work fine with globals(), but am I missing anything? More confusing is a class method. If I do something like this:</p>

<pre><code>a = 'test'

class Foo(object):
    b = 'Foo prints this'

    def myfunc():
        print(a, self.b)

Bar = type('Bar', tuple(), dict())
Bar.b = 'Bar prints this'

ns = globals()
#should I do ns.update(something) here?

f = Foo.myfunc
Bar.myfunc = types.FunctionType(f.__code__, ns, 'myfunc', ...)
</code></pre>

<p>That example works, but not this more complicated one:</p>

<pre><code>import imp
import builtins

class Foo(object):
    def __init__(self):
        super().__init__() # super(Foo, self) also fails

mod = imp.new_module('test')
mod.__builtins__ = builtins
mod.Foo = type('Foo', tuple(), dict())

f = Foo.__init__
ns = {}
ns.update(mod.__dict__) #missing something here

mod.Foo.__init__ = types.FunctionTypes(f.__code__, ns, '__init__', ...)
</code></pre>

<p>Could someone illuminate what should be in <code>ns</code>? What is available to a class method, and what isn't? </p>

<p>I'm not specifically trying to get the above code to work, I'm more looking for an explanation of why it doesn't.</p>
