如何在python中翻译这段代码以使其更具可读性?



我想翻译这段代码以提高可读性,但是当我尝试时,它不起作用。它在原始版本中有效,但在第二个版本中不起作用。

我用这种方式尝试过。

down, up = [(+1, -1), (+1, +1)], [(-1, -1), (-1, +1)]
length = board.get_length()
piece = board.get(row, col)
if piece:
for (x, y) in down:
if (0 <= (row + x) < length) and (0 <= (col + y) < length) and board.is_free(row + x, col + y):
bottom = [main.deindexify(row + x, col + y)]
# bottom = [main.deindexify(row + x, col + y) for (x, y) in down 
#           if (0 <= (row + x) < length)
#           and (0 <= (col + y) < length) 
#           and board.is_free(row + x, col + y)]
for (x, y) in up:
if (0 <= (row + x) < length) and (0 <= (col + y) < length) and board.is_free(row + x, col + y):
top = [main.deindexify(row + x, col + y)]
# top = [main.deindexify(row + x, col + y) for (x, y) in up 
#        if (0 <= (row + x) < length) 
#        and (0 <= (col + y) < length) 
#        and board.is_free(row + x, col + y)]
if piece.is_king():
return sorted(bottom + top)
else:
if piece.is_black():
return sorted(bottom)
else:
if is_sorted:
return sorted(top)
else:
if piece.is_king():
return bottom + top
else:
if piece.is_black():
return bottom
else:
return top
# return (sorted(bottom + top) if piece.is_king() else 
#             (sorted(bottom) if piece.is_black() else sorted(top))) 
#     if is_sorted else (bottom + top if piece.is_king() else 
#                            (bottom if piece.is_black() else top))
return []

这是一个错误:

line 61, in get_moves
return sorted(bottom)
UnboundLocalError: local variable 'bottom' referenced before assignment

这是原始版本。

down, up = [(+1, -1), (+1, +1)], [(-1, -1), (-1, +1)]
length = board.get_length()
piece = board.get(row, col)
if piece:
bottom = [main.deindexify(row + x, col + y) for (x, y) in down 
if (0 <= (row + x) < length) 
and (0 <= (col + y) < length) 
and board.is_free(row + x, col + y)]
top = [main.deindexify(row + x, col + y) for (x, y) in up 
if (0 <= (row + x) < length) 
and (0 <= (col + y) < length) 
and board.is_free(row + x, col + y)]
return (sorted(bottom + top) if piece.is_king() else 
(sorted(bottom) if piece.is_black() else sorted(top))) 
if is_sorted else (bottom + top if piece.is_king() else 
(bottom if piece.is_black() else top))
return []

有人可以解释我如何阅读和翻译这段代码,以便我可以重构其余代码吗?

如果xy满足某些条件,则以下代码段(来自原始代码)创建一个列表。它被写成一个列表理解。

....
bottom = [main.deindexify(row + x, col + y) for (x, y) in down 
if (0 <= (row + x) < length) 
and (0 <= (col + y) < length) 
and board.is_free(row + x, col + y)]

将列表推导(重构)展开到for循环中的一种方法可能是:

....
bottom = []
for (x,y) in down:
x_length = (0 <= (row + x) < length)
y_length = (0 <= (col + y) < length)
cel_isfree = board.is_free(row + x, col + y)
if x_length and ylength and cel_isfree:
bottom.append(main.deindexify(row + x, col + y))

top也可以通过这种方式重构。

以下一段原始代码根据几个条件确定返回值 -的类型以及是否排序。它被编写为嵌套条件表达式。

....
return (sorted(bottom + top) if piece.is_king() else 
(sorted(bottom) if piece.is_black() else sorted(top))) 
if is_sorted else (bottom + top if piece.is_king() else 
(bottom if piece.is_black() else top))

它可以像这样重构:

....
if is_sorted:
if piece.is_king():
return_value = sorted(bottom + top)
elif piece.is_black():
return_value = sorted(bottom)
else:
return_value = sorted(top)
else:
if piece.is_king():
return_value = bottom + top
elif piece.is_black():
return_value = bottom
else:
return_value = top
return return_value

当然,我没有一个简单的方法来测试这一点。

最新更新