我正在尝试为我的项目定义一个'util'模块,其中基本的util函数是'display windows'(在这种情况下为opencv项目:]
def display_windows(windows_to_display, with_text=False):
for window_tag, window in windows_to_display:
cv2.imshow(window_tag, window)
除了基本的“显示”,我想有多个选择,例如
向窗口添加文本(cv2.putText()
显示窗口后等待(cv2.waitKey())
我想知道如何解决这个问题,一种选择是使用默认值添加多个布尔值,然后添加类似以下内容:
def display_windows(windows_to_display, with_text=False, with_wait=False, destroy_first=False):
if destroy_first:
cv2.destroyAllWindows()
for window_tag, window in windows_to_display:
if with_text:
cv2.putText(window, 'text', ...)
cv2.imshow(window_tag, window)
if with_wait:
cv2.waitKey(0)
或者,我可以声明几个函数,使用'base''display windows'然后添加说:
def display_windows_and_wait(self, windows_to_display, with_text=False, destroy_first=False):
display_windows(windows_to_display, with_text)
cv2.waitKey(0)
等
我对任何一种选择都不完全满意;使用布尔方法,我不是拥有:
的忠实粉丝display_windows(windows, True, True, True)
分散,因为它不是很有用。
使用多功能方法,很好,不确定是否真的有帮助。
关于哪种方法可以提高可读性的任何想法?甚至更好,是否有更好的方法来解决此问题?
非常感谢
代替通话