以下是您新发布的代码的一个真实示例:
import types
def attach_on_sample_slider(obj, base):
def on_sample_slider(self, value):
self.samples = base**value
self.sample_label.setText('%d' % self.samples)
# This next line creates a method from the function
# The first arg is the function and the second arg is the object
# upon which you want it to be a method.
obj.on_sample_slider = types.MethodType(on_sample_slider, obj)
你现在可以这样称呼它
def some_method(self, foo):
attach_on_sample_slider(self, 4)
原帖
既然你说成员函数是相同的,我会这样做
def make_method(name):
def method(self, whatever, args, go, here):
#whatever code goes here
method.__name__ = name
return method
class A(object):
method1 = make_method('method1')
method2 = make_method('method2')
严格来说,传递名字和设置
__name__
新函数的属性不是必需的,但它可以帮助调试。它有点重复,而且可以自己付费。如果你想跳过这一点,你也可以跳过。
class A(object):
def method1(self, arg1, arg2):
#code goes here
method2 = method1
method3 = method1
这将创建相同的方法。调用它们中的任何一个都将产生相同的方法。
第一种形式更强大,因为您可以将除名称之外的其他参数传递到
make_method
并且让返回方法的不同版本在闭包中访问这些参数,这样它们的工作方式就不同了。下面是一个关于函数的愚蠢例子(与方法相同):
def make_opener(filename):
def opener():
return open(filename)
return opener
open_config = make_opener('config.cfg')
open_log = make_opener('log.log')
在这里,它们基本上都是相同的功能,但是做的事情略有不同,因为它们可以访问
filename
它们是用它们创造的。如果你要做很多这样的事情,闭包绝对是值得研究的。
还有很多问题要解决,所以如果你有一些特定的问题没有解决,你应该更新你的问题。