为什么我要在 Python 中传递动态函数时遇到问题
why i face problem when i'm going to pass a dynamic function in Python
我要将一个函数动态传递给另一个class,如下所示
class simulator(object):
def __init__(self, fn_):
print(self.test(fn_))
def test(self, fn):
return fn(self, 20)
class t(object):
s = 'def get_fitness(x, y):\n return x+y'
exec(s)
def fnGetFitness(self,genes):
return get_fitness(genes, 10)
simulator(fnGetFitness)
t()
但我遇到以下错误:
File "N:/Job/GA/mine/dyn.py", line 25, in fnGetFitness
return get_fitness(genes, 10)
NameError: name 'get_fitness' is not defined
我猜它与范围有关,但我无法处理它
有人知道吗?
编辑:
这是一个更简单的代码,显示了问题:
class t(object):
def __init__(self):
exec('def get_fitness(x, y):\n return x+y')
print(get_fitness(2,3))
t()
与 exec
无关。你所做的等同于(删除安全):
class t(object):
def get_fitness(x,y):
return x+y
但是你的方法定义是在class级别,而不是simulator
class。
simulator(fnGetFitness)
从 t
class 上下文中调用 fnGetFitness
,因此它不知道您的新函数。
那行不通(另外 get_fitness
应该装饰成 @staticmethod
因为它没有 self
参数)
有效的是在全局级别动态定义(或不定义)函数,以便class可以调用它
s = 'def get_fitness(x, y):\n return x+y'
exec(s)
class t(object):
def fnGetFitness(self,genes):
return get_fitness(genes, 10)
simulator(fnGetFitness)
t()
修复了它,但老实说我对它的目的感到困惑(我已经花了一段时间才弄清楚如何从你的代码中做出一些东西 运行)
编辑:评论中发布了一个更简单且有所不同(和 exec
相关)的代码:
class t(object):
def __init__(self):
exec('def get_fitness(x, y):\n return x+y')
print(get_fitness(2,3))
t()
这会引发 NameError: name 'get_fitness' is not defined
现在这与exec
有关。当 __init__
被解析时,get_fitness
是未知的,因为解析器没有将其视为局部变量,即使在执行时,它在 locals()
字典中设置为exec
(相关:)。
解决方法是像这样在局部变量中获取函数:
class t(object):
def __init__(self):
exec('def get_fitness(x, y):\n return x+y')
print(locals()["get_fitness"](2,3))
t()
这有效并打印 5
。
我要将一个函数动态传递给另一个class,如下所示
class simulator(object):
def __init__(self, fn_):
print(self.test(fn_))
def test(self, fn):
return fn(self, 20)
class t(object):
s = 'def get_fitness(x, y):\n return x+y'
exec(s)
def fnGetFitness(self,genes):
return get_fitness(genes, 10)
simulator(fnGetFitness)
t()
但我遇到以下错误:
File "N:/Job/GA/mine/dyn.py", line 25, in fnGetFitness
return get_fitness(genes, 10)
NameError: name 'get_fitness' is not defined
我猜它与范围有关,但我无法处理它 有人知道吗?
编辑:
这是一个更简单的代码,显示了问题:
class t(object):
def __init__(self):
exec('def get_fitness(x, y):\n return x+y')
print(get_fitness(2,3))
t()
与 exec
无关。你所做的等同于(删除安全):
class t(object):
def get_fitness(x,y):
return x+y
但是你的方法定义是在class级别,而不是simulator
class。
simulator(fnGetFitness)
从 t
class 上下文中调用 fnGetFitness
,因此它不知道您的新函数。
那行不通(另外 get_fitness
应该装饰成 @staticmethod
因为它没有 self
参数)
有效的是在全局级别动态定义(或不定义)函数,以便class可以调用它
s = 'def get_fitness(x, y):\n return x+y'
exec(s)
class t(object):
def fnGetFitness(self,genes):
return get_fitness(genes, 10)
simulator(fnGetFitness)
t()
修复了它,但老实说我对它的目的感到困惑(我已经花了一段时间才弄清楚如何从你的代码中做出一些东西 运行)
编辑:评论中发布了一个更简单且有所不同(和 exec
相关)的代码:
class t(object):
def __init__(self):
exec('def get_fitness(x, y):\n return x+y')
print(get_fitness(2,3))
t()
这会引发 NameError: name 'get_fitness' is not defined
现在这与exec
有关。当 __init__
被解析时,get_fitness
是未知的,因为解析器没有将其视为局部变量,即使在执行时,它在 locals()
字典中设置为exec
(相关:
解决方法是像这样在局部变量中获取函数:
class t(object):
def __init__(self):
exec('def get_fitness(x, y):\n return x+y')
print(locals()["get_fitness"](2,3))
t()
这有效并打印 5
。