Python 函数名作为另一个函数的输入?
我是一名图像处理工程师,正在使用Python作为原型语言 大多数时候,当我得到数千张名为“imagen.jpg”的图像时,n是增量 因此,我的程序的主要结构可以看作:Python 函数名作为另一个函数的输入?,python,Python,我是一名图像处理工程师,正在使用Python作为原型语言 大多数时候,当我得到数千张名为“imagen.jpg”的图像时,n是增量 因此,我的程序的主要结构可以看作: def main_IP(imgRoot, stop_ncrement): name = update_the_increment(imgRoot, stop_increment) img = load_the_image(name) out_img = process_image(img) displays_ima
def main_IP(imgRoot, stop_ncrement):
name = update_the_increment(imgRoot, stop_increment)
img = load_the_image(name)
out_img = process_image(img)
displays_images(img, out_img)
return out_img
正如您所看到的,从一个应用程序到另一个应用程序,唯一的变化是process_image函数。
是否有一种方法可以将过程图像作为输入插入
我将得到一个通用函数,原型为:
主IP(imgRoot、停止增量、处理映像)
谢谢!
Julien可以像字符串或任何其他对象一样在python中传递函数
def processImage(...):
pass
def main_IP(imgRoot, stop_ncrement, process_image):
name = update_the_increment(imgRoot, stop_increment)
img = load_the_image(name)
out_img = process_image(img)
displays_images(img, out_img)
return out_img
main_IP('./', 100, processImage)
是的,在python中,函数是一级对象,因此您可以像任何其他数据类型一样将它们作为参数传递。以下是一些代码,演示如何传递要调用的函数的名称,以及传递对要调用的函数的引用:
def A():
return "A!"
def B():
return "B!"
def CallByName(funcName):
return globals()[funcName]()
def CallByReference(func):
return func()
print CallByName("A")
functionB = B
print CallByReference(functionB)
谢谢最后没那么复杂:)