可以使用全局变量来作为另一个函数的参数吗?

huangapple go评论71阅读模式
英文:

Can I use a global variable to parameter another function?

问题

我有一个bbox_sort函数,用于按从左到右的顺序排序坐标。为了考虑两个边界框之间的Y高度,我启动了thresh变量。在get_predict函数中,bbox_sort函数被作为参数传递给cmp_to_key。我需要使thresh成为可选参数。

我知道使用全局变量是不良实践,但我不知道如何将thresh参数传递给bbox_sort函数。将数值thresh写入一个单独的文件并从那里获取它是否是良好实践,或者我应该如何重新制作排序函数?

英文:

I have a bbox_sort function to sort the coordinates in order from left to right. To take into account the Y height between the two bounding bbox, I start the thresh variable. In get_predict , the bbox_sort function is passed as an argument to cmp_to_key. I need to make optional parameter thresh

I know it's bad practice to use global variables, but I don't know how else to pass the thresh parameter to the bbox_sort function. Is it good practice to write the number thresh to a separate file and get it from there or how can I remake the sort function?

from functools import cmp_to_key

def bbox_sort(a, b,):

    bbox_a = a
    bbox_b = b
    
    if abs(bbox_a[1] - bbox_b[1]) <= thresh: 
        return bbox_a[0] - bbox_b[0]
    
    return bbox_a[1] - bbox_b[1]

def get_prediction(result):
    coord_list = []
    res = result.to_coco_annotations()

    global thresh
    thresh = 10
    for ann in res:

        current_bbox = ann['bbox']
        x = current_bbox[0]
        y = current_bbox[1]
        w = current_bbox[2]
        h = current_bbox[3]
    
        coord_list.append((x, y, w, h))

    cnts = sorted(coord_list, key=cmp_to_key(bbox_sort))
    for pred in range(len(res)-1):
        res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))

    return res

答案1

得分: 1

你可以创建一个名为 `create_bbox_sort` 的函数它会根据传递给 `create_bbox_sort``thresh` 参数动态创建一个 `bbox_sort` 函数这样你就不需要使用全局变量而且 `create_bbox_sort` 函数可以轻松地在其他地方和其他脚本中使用这类似于 [带属性的装饰器][1] 的工作方式


新代码如下

```python
from functools import cmp_to_key

def create_bbox_sort(thresh):

    def bbox_sort(a, b,):

        bbox_a = a
        bbox_b = b
        
        if abs(bbox_a[1] - bbox_b[1]) <= thresh: 
            return bbox_a[0] - bbox_b[0]
        
        return bbox_a[1] - bbox_b[1]
    
    return bbox_sort

def get_prediction(result):
    coord_list = []
    res = result.to_coco_annotations()

    thresh = 10
    for ann in res:

        current_bbox = ann['bbox']
        x = current_bbox[0]
        y = current_bbox[1]
        w = current_bbox[2]
        h = current_bbox[3]
    
        coord_list.append((x, y, w, h))

    cnts = sorted(coord_list, key=cmp_to_key(create_bbox_sort(thresh)))
    for pred in range(len(res)-1):
        res[pred]['image_id'] = cnts.index(tuple(res[pred]['bbox']))

    return res

<details>
<summary>英文:</summary>

You can create a `create_bbox_sort` function which creates a `bbox_sort` function on the fly using the `thresh` passed as a parameter to `create_bbox_sort`. This way, you don&#39;t need a global variable and the `create_bbox_sort` function can easily be used elsewhere and in other scripts. This is similar to the way [decorators with attributes][1] work.


The new code is :

```python
from functools import cmp_to_key

def create_bbox_sort(thresh):

    def bbox_sort(a, b,):

        bbox_a = a
        bbox_b = b
        
        if abs(bbox_a[1] - bbox_b[1]) &lt;= thresh: 
            return bbox_a[0] - bbox_b[0]
        
        return bbox_a[1] - bbox_b[1]
    
    return bbox_sort

def get_prediction(result):
    coord_list = []
    res = result.to_coco_annotations()

    thresh = 10
    for ann in res:

        current_bbox = ann[&#39;bbox&#39;]
        x = current_bbox[0]
        y = current_bbox[1]
        w = current_bbox[2]
        h = current_bbox[3]
    
        coord_list.append((x, y, w, h))

    cnts = sorted(coord_list, key=cmp_to_key(create_bbox_sort(thresh)))
    for pred in range(len(res)-1):
        res[pred][&#39;image_id&#39;] = cnts.index(tuple(res[pred][&#39;bbox&#39;]))

    return res

答案2

得分: 0

If your threshold is a global variable only configurable by the developer it's common practice to define it on top of the file written in all caps THRESHOLD = 10 right under your imports.

In python you only need to use global ... when you edit a global variable inside a function. (Which you don't plan on doing here, I assume.)

If the threshold isn't constant, and you want to be able to change it at runtime, you can wrap your function in a lambda like that:

cnts = sorted(coord_list, key=cmp_to_key(lambda a,b: bbox_sort(a, b, thresh)))

And then add a threshold to your arguments:

def bbox_sort(a, b, threshold):

英文:

If your threshold is a global variable only configurable by the developer it's common practice to define it on top of the file written in all caps THRESHOLD = 10 right under your imports.

In python you only need to use global ... when you edit a global variable inside a function. (Which you don't plan on doing here, I assume.)

If the threshold isn't constant, and you want to be able to change it at runtime, you can wrap your function in a lambda like that:

cnts = sorted(coord_list, key=cmp_to_key(lambda a,b: bbox_sort(a, b, thresh)))

And then add a threshold to your arguments:

def bbox_sort(a, b, threshold):

huangapple
  • 本文由 发表于 2023年2月23日 19:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定