我正在尝试维护/更新/重写/修复一些看起来有点像这样的Python:
variable = """My name is %s and it has been %s since I was born.
My parents decided to call me %s because they thought %s was a nice name.
%s is the same as %s.""" % (name, name, name, name, name, name)
脚本中到处都是这样的小片段,我想知道是不是有更简单的(更Pythonic的?)写这段代码的方式。我发现了一个这样的实例,它将相同的变量替换了大约30次,感觉很难看。
在我看来,绕过丑陋的唯一方法是把它分成许多小块吗?
variable = """My name is %s and it has been %s since I was born.""" % (name, name)
variable += """My parents decided to call me %s because they thought %s was a nice name.""" % (name, name)
variable += """%s is the same as %s.""" % (name, name)
https://stackoverflow.com/questions/6982949
复制相似问题