我有一个手动维护的python函数和模块列表,将由遗传算法和 我希望能够反复调用这些并监视结果。
import os
from random import randint
def main():
toolList = [{'file':'test1.py', 'function':'sum_even_numbers', 'args':['list'], 'return':['int']},
{'file':'test1.py', 'function':'get_min_even_num', 'args':['list'], 'return':['int']} ]
for tool in toolList:
for i in range(0,3):
run(tool, [randint(10,99) for j in range(1,randint(2,5))])
def run(tool, args):
mod = __import__( os.path.basename(tool['file']).split('.')[0])
func = getattr(mod, tool['function'])
tool['return'] = func(args)
print('main called ' + tool['file'] + '->' + tool['function'] + ' with ', args, ' = ', tool['return'])
main()
def sum_even_numbers(numbers):
return sum([i for i in numbers if i % 2 == 0])
def get_min_even_num(numbers):
return min([i for i in numbers if i % 2 == 0])
# other code in test1.py which we dont want to execute
print('test1.py has been run' )
代码的结果:
test1.py has been run
('main called test1.py->sum_even_numbers with ', [30, 66, 25, 45], ' = ', 96)
('main called test1.py->sum_even_numbers with ', [92], ' = ', 92)
('main called test1.py->sum_even_numbers with ', [59, 73], ' = ', 0)
('main called test1.py->get_min_even_num with ', [59, 12, 61, 24], ' = ', 12)
('main called test1.py->get_min_even_num with ', [22], ' = ', 22)
('main called test1.py->get_min_even_num with ', [97, 94], ' = ', 94)
代码通过字符串调用模块中的函数来工作,但在导入期间执行整个文件。 有没有更好的方法来执行此操作,以便不运行整个模块?
答案 0 :(得分:4)
当您从模块导入内容时 - 甚至是独立的模块级函数 - python解释器具有来解析/编译整个文件,然后才能访问该函数。
在def
s的情况下,这是无害的 - 它们只是被编译成函数,没有造成任何伤害。但是,有时你会有"代码,你不想运行",就像你的例子中的print
一样。
导入模块时阻止代码执行的规范方法是:
if __name__ == '__main__':
# code that only gets executed when I explicitly run this module