Python: 如何改进重复代码?

Python: How can I improve the repetitive code?

我有一个代码 python。

我的代码运行完美,但我想改进它,因为有重复的代码。

这是我的代码:

x = int(x)
y = int(y)
w = int(w)
h = int(h)

如何改进重复代码?

目前的代码也可读性好,但我想知道 Python 语法和 Python 风格。

请多多指教

我个人认为保留原样没什么大不了的,但如果您愿意,可以像这样使用地图:

x, y, w, h = map(int, (x, y, w, h))

或者,映射到第一行:

x, y, w, h = map(int, cv.boundingRect(biggest_contour) * np.array([ratio, ratio, ratio, ratio]))

如果您将 x,y,w,h 放入列表中,您可以将其迭代为 simplify/shorten 代码,例如

myList = [x,y,w,h]
myList = [int(cv.boundingRect(biggest_contour) * np.array([ratio, ratio, ratio, ratio]) for item in myList]

我会这样做:

x,y,w,h = [int(i) for i in [x,y,w,h]]