开发者

Is this bad practice?

In this piece of code, if I match the given pattern on the line, I replace the line by a self-built string. Is this actually good practice? It looks a little bit like reusing the variable line for output, where it actually contains the contents of the currently read line.

for line in lines:
  match = re.search(r'@Table\(name = "(.*)"\)', line)

  if match:
    line = "".join(['@Table (name = "', prefix, match.group(1)[:max_len], '")', '\n'])

  开发者_运维知识库f.write(line)

f.close()


I'd say the intent in your code is clear and the code is short and simple so there is nothing wrong in it. If it still bothers you to be reusing the variable, you can do something like this:

for line in lines:
    match = re.search(r'@Table\(name = "(.*)"\)', line)

    if match:
        output_line = "".join(['@Table (name = "', prefix, match.group(1)[:max_len], '")', '\n'])
    else:
        output_line = line

    f.write(output_line)

f.close()

This way, each variable name describes exactly its contents all of the time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜