【问题标题】:How to pass functions with arguments as parameter to another function in python with function name as a string?python - 如何将带有参数的函数作为参数传递给python中函数名作为字符串的另一个函数?
【发布时间】:2019-04-23 15:32:14
【问题描述】:

我在 main.py 中有以下源代码

main.py

import data_utils as du

X_train1, Y_train1, groundtruth_train1= du.loaddata(train_file, "adjust_gamma", 0.8)
X_train2, Y_train2, groundtruth_train2= du.loaddata(train_file, "adjust_gamma", 1.2)
X_train3, Y_train3, groundtruth_train3= du.loaddata(train_file, "scale_image", 0.5)
X_train4, Y_train4, groundtruth_train4= du.loaddata(train_file, "scale_image", 0.8)
X_train5, Y_train5, groundtruth_train5= du.loaddata(train_file, "scale_image", 1.5)
X_train6, Y_train6, groundtruth_train6= du.loaddata(train_file, "scale_image", 2.0)
X_train7, Y_train7, groundtruth_train7= du.loaddata(train_file, "compress_jpeg", 70)
X_train8, Y_train8, groundtruth_train8= du.loaddata(train_file, "compress_jpeg", 90)

main.py 将读取多个图像,应用图像转换,将它们分成块(这些是我的 X_train 输出)并获取图像标签(Y_train 和 groundtruth_train)。图像变换由带有参数的字符串定义(例如,“adjust_gamma”等)。

现在,load_data.py 的构建灵感来自这个答案about how to pass functions with arguments to another function

data_utils.py

def loaddata(file_images, function_name, parameter):

x = []
y = []

with open(file_images) as f:
    images_names = f.readlines()
    images_names = [a.strip() for a in images_names]

    j=0

    for line in images_names:

        j=j+1

        line='image_folder/' + line
        img = cv2.imread(img_path)
        img=perform(eval(function_name)(img,parameter)) 
        ...

函数执行将接收函数名称(如您在 main.py 中看到的字符串)及其参数(numpy 数组和参数中的图像)。执行的功能详述如下:

def perform(fun, *args):
fun(*args)

def adjust_gamma(image, gamma):
   invGamma = 1.0 / gamma
   table = np.array([((i / 255.0) ** invGamma) * 255
      for i in np.arange(0, 256)]).astype("uint8")
   return cv2.LUT(image, table)

def compress_jpeg(image, compression_factor):
   encode_param=[int(cv2.IMWRITE_JPEG_QUALITY), compression_factor]
   result, encimg=cv2.imencode('.jpg', image, encode_param)
   decimg=cv2.imdecode(encimg, 1)
   return decimg

def scale_image(image, scaling_factor):
   resized_image=cv2.resize(image, (scaling_factor, scaling_factor))
   return resized_image 

我尝试使用 eval 函数,因此传递的字符串可以视为函数名(This answer inspired me to do that)。但是,当我运行代码时,出现以下错误:

文件 “主.py”, 第 32 行,在 do_experiment 中

X_train1, Y_train1, groundtruth_train1= du.loaddata(train_file, "adjust_gamma", 0.8)

文件 "data_utils.py", 第 29 行,在加载数据中

img=perform(eval(function_name)(img,parameter)) 文件“data_utils.py”,

第 171 行,执行中 fun(**args) TypeError: 'numpy.ndarray' 对象不可调用

那么,我该如何解决我的问题?如何使用函数名作为字符串将函数作为参数传递给另一个函数?

【问题讨论】:

  • 为什么要对函数使用字符串别名而不是导入它们?
  • 您的缩进似乎已关闭。不要让我们猜测应该缩进什么,请edit 你的问题告诉我们每个def 结束的确切位置。 (在此站点的桌面版本中,复制粘贴您的代码,选择粘贴的文本,然后键入 ctrl-K 使其缩进。)

标签: python string function


【解决方案1】:

eval(function_name)(img,parameter) 替换为:

globals()[function_name](img,parameter)

请注意,您想要的函数应该在我的答案中的同一模块中,如果不是,请阅读 this linkThis 关于 python 中的 globalslocals 以找到最适合您问题的东西。

另外,您可以使用getattr 访问另一个模块的功能,如下所示:

getattr(module, func)(*args, **kwargs)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多