返回带有for循环的函数列表的python函数
我试图实现一个函数(make_q),它返回一个函数列表(q),这些函数是使用make_q获取(p)的参数生成的。Q是一个依赖于n(=len(P))的变量,使得Q函数是相似的,所以它可以在for循环中完成,但这里有一个问题,如果我在循环中命名函数,它们都有相同的地址,所以我只得到最后一个Q,有没有绕过这个问题? 这是我的密码返回带有for循环的函数列表的python函数,python,python-3.x,function,Python,Python 3.x,Function,我试图实现一个函数(make_q),它返回一个函数列表(q),这些函数是使用make_q获取(p)的参数生成的。Q是一个依赖于n(=len(P))的变量,使得Q函数是相似的,所以它可以在for循环中完成,但这里有一个问题,如果我在循环中命名函数,它们都有相同的地址,所以我只得到最后一个Q,有没有绕过这个问题? 这是我的密码 def make_q(self): Temp_P=[p for p in self.P] Q=() for i in ran
def make_q(self):
Temp_P=[p for p in self.P]
Q=()
for i in range(self.n-1):
p=min(Temp_P)
q=max(Temp_P)
index_p=Temp_P.index(p)
index_q=Temp_P.index(q)
def tempQ():
condition=random.random()
if condition<=(p*self.n):
return index_p
else:
return index_q
Temp_Q=list(Q)
Temp_Q.append(tempQ)
Q=tuple(Temp_Q)
q-=(1-p*self.n)/self.n
Temp_P[index_q]=q
Temp_P.pop(index_p)
return Q
def make_q(自):
Temp_P=[P代表self.P中的P]
Q=()
对于范围内的i(自身n-1):
p=最小值(温度p)
q=最大值(温度)
索引p=临时索引(p)
索引q=临时索引(q)
def tempQ():
条件=随机。随机()
如果条件请尝试通过格式化字符串,例如:“function{}.format(name)”也可以,您希望您的输出是什么样子?jasonharper在注释中的观察结果和解决方案是正确的(应该是公认的答案)。但既然你问起了元类,我还是发了这个帖子
在python中,每个类都是类型
,带有“name”、“base”(基类)和“attrs”(类的所有成员)。本质上,元类定义了类的行为,您可以在和各种其他在线教程中阅读更多关于它的内容
\uuuu new\uuu
方法在设置类时运行。注意attrs
的用法,其中您的类成员self.n
由attrs['n']
访问(因为attrs是所有类成员的dict)。我正在动态定义函数tempQ\u 0、tempQ\u 1…
。如您所见,我们还可以向这个动态定义的类成员添加docstring
import random
class MyMetaClass(type):
def __new__(cls, name, bases, attrs):
Temp_P = [p for p in attrs['P']]
for i in range(attrs['n'] - 1):
p = min(Temp_P)
q = max(Temp_P)
index_p = Temp_P.index(p)
index_q = Temp_P.index(q)
def fget(self, index_p=index_p, index_q=index_q): # this is an unbound method
condition = random.random()
return index_p if condition <= (p * self.n) else index_q
attrs['tempQ_{}'.format(i)] = property(fget, doc="""
This function returns {} or {} randomly""".format(index_p, index_q))
q -= (1 - p * attrs['n']) / attrs['n']
Temp_P[index_q] = q
Temp_P.pop(index_p)
return super(MyMetaClass, cls).__new__(cls, name, bases, attrs)
# PY2
# class MyClass(object):
# __metaclass__ = MyMetaClass
# n = 3
# P = [3, 6, 8]
# PY3
class MyClass(metaclass=MyMetaClass):
n = 3
P = [3, 6, 8]
# or use with_metaclass from future.utils for both Py2 and Py3
# print(dir(MyClass))
print(MyClass.tempQ_0, MyClass.tempQ_1)
随机导入
类MyMetaClass(类型):
定义(cls、名称、基数、属性):
温度P=[P代表属性中的P['P']]
对于范围内的i(属性['n']-1):
p=最小值(温度p)
q=最大值(温度)
索引p=临时索引(p)
索引q=临时索引(q)
def fget(self,index_p=index_p,index_q=index_q):#这是一个未绑定的方法
条件=随机。随机()
如果您可以使用元类生成带有变量的动态函数,则返回索引pnames@vestronge我从来没有听说过元类,你能解释更多吗?所有函数都有相同的名称,并且它们实际上没有相同的地址,这没有问题。问题是它们都在访问相同的index\u p
和index\u q
值,这将是make\u q()
中最后一次循环迭代的值。您需要在定义函数时捕获这些值:一种方法是将它们作为默认参数传递给生成的函数,在Python中,这些函数在定义时进行计算:def tempQ(index\u p=index\u p,index\u q=index\u q):
。给我一些时间。让我补充一些解释和建议solution@jasonharper真的是这样!thanksI只需要输出一组易怒的函数
import random
class MyMetaClass(type):
def __new__(cls, name, bases, attrs):
Temp_P = [p for p in attrs['P']]
for i in range(attrs['n'] - 1):
p = min(Temp_P)
q = max(Temp_P)
index_p = Temp_P.index(p)
index_q = Temp_P.index(q)
def fget(self, index_p=index_p, index_q=index_q): # this is an unbound method
condition = random.random()
return index_p if condition <= (p * self.n) else index_q
attrs['tempQ_{}'.format(i)] = property(fget, doc="""
This function returns {} or {} randomly""".format(index_p, index_q))
q -= (1 - p * attrs['n']) / attrs['n']
Temp_P[index_q] = q
Temp_P.pop(index_p)
return super(MyMetaClass, cls).__new__(cls, name, bases, attrs)
# PY2
# class MyClass(object):
# __metaclass__ = MyMetaClass
# n = 3
# P = [3, 6, 8]
# PY3
class MyClass(metaclass=MyMetaClass):
n = 3
P = [3, 6, 8]
# or use with_metaclass from future.utils for both Py2 and Py3
# print(dir(MyClass))
print(MyClass.tempQ_0, MyClass.tempQ_1)
<property object at 0x10e5fbd18> <property object at 0x10eaad0e8>