<p>How do I add an instance method to a class using a metaclass (yes I do need to use a metaclass)? The following kind of works, but the func_name will still be "foo":</p>

<pre><code>def bar(self):
    print "bar"

class MetaFoo(type):
    def __new__(cls, name, bases, dict):
        dict["foobar"] = bar
        return type(name, bases, dict)

class Foo(object):
    __metaclass__ = MetaFoo

&gt;&gt;&gt; f = Foo()
&gt;&gt;&gt; f.foobar()
bar
&gt;&gt;&gt; f.foobar.func_name
'bar'
</code></pre>

<p>My problem is that some library code actually uses the func_name and later fails to find the 'bar' method of the Foo instance. I could do:</p>

<pre><code>dict["foobar"] = types.FunctionType(bar.func_code, {}, "foobar")
</code></pre>

<p>There is also types.MethodType, but I need an instance that does'nt exist yet to use that. Am I missing someting here?</p>

I think what you want to do is this:
```
```
>>> class Foo():
...   def __init__(self, x):
...     self.x = x
... 
>>> def bar(self):
...   print 'bar:', self.x
... 
>>> bar.func_name = 'foobar'
>>> Foo.foobar = bar
>>> f = Foo(12)
>>> f.foobar()
bar: 12
>>> f.foobar.func_name
'foobar'
```
```
Now you are free to pass
```
Foo
```
s to a library that expects
```
Foo
```
instances to have a method named
```
foobar
```
.
Unfortunately, (1) I don't know how to use metaclasses and (2) I'm not sure I read your question correctly, but I hope this helps.
Note that
```
func_name
```
is only assignable in Python 2.4 and higher.