1

假设我有一个名字列表。我希望能够将这些作为实例函数动态添加到类实例中。我知道 types.MethodType 但从那里到这里我有点新手。基本上我想做的是:

class foo( object ):
     def __init__(self):
         pass

f = foo()
names = ["a","b","c"]
for name in names:
    add name() to f  # not sure what to do here
    # what I wanted added to instance "f" is this for each name:
    def name(self, *args, **kwargs):
        print( "My name is %s" % inspect.stack()[0][3]   )       
        print( "__called, args=%r, **kwargs=%r" % (args, kwargs) )

f.a() # ==> calls f.a()
f.b(1,2,3) # calls f.b(1,2,3 )and so on
4

2 回答 2

5

如果您将函数创建为嵌套函数,则更容易;name然后是从嵌套范围中获取的变量:

import types
def buildMethod(name, instance):
    def namefunc(self, *args, **kwargs):
        print "My name is %s" % name
        print "__called, args=%r, **kwargs=%r" % (args, kwargs)
    namefunc.__name__ = name
    setattr(instance, name, types.MethodType(namefunc, instance, type(instance)))

f = foo()
names = ["a","b","c"]
for name in names:
    buildMethod(name, f)

这导致:

>>> f.a
<bound method instance.a of <__main__.foo instance at 0x100d8e560>>
>>> f.a.__name__
'a'
>>> f.a()
My name is a
__called, args=(), **kwargs={}
>>> f.b()
My name is b
__called, args=(), **kwargs={}
>>> f.c()
My name is c
__called, args=(), **kwargs={}
于 2012-11-30T18:23:45.137 回答
1

我错了,setattr独自一人就足够了。但是,这有效:

import inspect
import types

class foo( object ):
     def __init__(self):
         pass

def namefunc(self, *args, **kwargs):
    print( "My name is %s" % inspect.stack()[0][3]   )       
    print( "__called, args=%r, **kwargs=%r" % (args, kwargs) )

f = foo()
names = ["a","b","c"]
for name in names:
    setattr(f, name, types.MethodType(namefunc, f))

f.a() # ==> calls f.a()
f.b(1,2,3) # calls f.b(1,2,3 )and so on
于 2012-11-30T18:17:29.690 回答