如何将对象的引用传递给函数以供重用

2024-03-29 12:55:08 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的第一个Python应用程序中,我试图遵循DRY原则(我是一个经验丰富的.NET开发人员)。我已经能够将大部分重复的代码转移到可重用的函数中。你知道吗

例如,下面是如何为matplotlib绘图创建线(边界框):

def generate_bounding_box_polygon(comma_delimited_rect: str):
    box_coordinates = comma_delimited_rect.strip().split(',')
    x = int(box_coordinates[0].strip())
    y = int(box_coordinates[1].strip())
    width = int(box_coordinates[2].strip())
    height = int(box_coordinates[3].strip())
    bottom_left = [x, y]
    bottom_right = [x + width, y]
    top_left = [x, y + height]
    top_right = [x + width, y + height]
    points = [bottom_left, top_left, top_right, bottom_right, bottom_left]
    polygon = plt.Polygon(points, fill=None, edgecolor='xkcd:rusty red', closed=False)
    return polygon

我在为我的绘图创建边界框时重用了它。这个嵌套的for循环位于多个函数中,因此使用generate_bounding_boxes函数是非常好的

for region in result["regions"]:
    region_box = generate_bounding_box_polygon(region["boundingBox"])
    plt.gca().add_line(region_box)

    for line in region["lines"]:
        line_box = generate_bounding_box_polygon(line["boundingBox"])
        plt.gca().add_line(line_box)

        for word in line["words"]:
            detected_text += word
            word_box = generate_bounding_box_polygon(word["boundingBox"])
            plt.gca().add_line(word_box)

            # RELEVANT  this is the code I want to move into a function
            box_coordinates = word["boundingBox"].strip().split(',')
            x = int(box_coordinates[0].strip())
            y = int(box_coordinates[1].strip())
            plt.gca().text(x, y-10, word["text"], fontsize=8)

但是,请注意最后一个代码注释,我还想将text方法移到函数中,但我需要对plt.gca()的引用

如何将其作为参数传递给函数?我尝试了下面的方法(参见第二个参数plot),就像我在C中所做的那样,但它不起作用,在python中可能是不好的做法:

def render_text(comma_delimited_rect: str, plot: matplotlib.pyplot):
    box_coordinates = comma_delimited_rect.strip().split(',')
    x = int(box_coordinates[0].strip())
    y = int(box_coordinates[1].strip())
    plt.gca().text(x, y-10, word["text"], fontsize=8)

注:plt定义为import matplotlib.pyplot as plt


Tags: 函数textboxlinepltleftgenerateword
2条回答

如果在函数内部使用plt.gca(),则不需要额外的参数。你知道吗

def render_text(comma_delimited_rect):
    box_coordinates = comma_delimited_rect.strip().split(',')
    x = int(box_coordinates[0].strip())
    y = int(box_coordinates[1].strip())
    plt.gca().text(x, y-10, word["text"], fontsize=8)

相反,如果您希望传递要打印的轴,则应将其提供给函数

def render_text(comma_delimited_rect, word, axes):
    box_coordinates = comma_delimited_rect.strip().split(',')
    x = int(box_coordinates[0].strip())
    y = int(box_coordinates[1].strip())
    axes.text(x, y-10, word, fontsize=8)

用例如

render_text( word["boundingBox"],  word["text"], plt.gca())

您还没有显示您正在使用的实际代码,但我认为您需要的是这样的代码:

def render_text(comma_delimited_rect: str, plot: matplotlib.pyplot):
    box_coordinates = comma_delimited_rect.strip().split(',')
    x = int(box_coordinates[0].strip())
    y = int(box_coordinates[1].strip())
    plot().text(x, y-10, word["text"], fontsize=8)

。。。打电话给。。。你知道吗

render_text(word["boundingBox"], plt.gca)

请注意如何将绘图函数作为函数传递,而不是作为无括号调用的结果传递。你知道吗

相关问题 更多 >