有条件的话改进Python
Improve Python if conditional
Python 3.6 中的条件如何改进以下内容。
def run_cmd(beta: bool): {
cmd = "cloud create {}".format(self.name)
if beta:
cmd = "cloud beta create {}".format(self.name)
}
实现此目的的一种方法:
cmd = "cloud{} create {}".format(["", " beta"][beta], self.name)
下面一个班轮将实现它:
cmd = "cloud {}create {}".format("beta " if beta else "", name)
如果你想减少if语句:
name = ''
beta = True
cmd = "cloud beta create {}".format(name) if beta else "cloud create {}".format(name)
Python 3.6 中的条件如何改进以下内容。
def run_cmd(beta: bool): {
cmd = "cloud create {}".format(self.name)
if beta:
cmd = "cloud beta create {}".format(self.name)
}
实现此目的的一种方法:
cmd = "cloud{} create {}".format(["", " beta"][beta], self.name)
下面一个班轮将实现它:
cmd = "cloud {}create {}".format("beta " if beta else "", name)
如果你想减少if语句:
name = ''
beta = True
cmd = "cloud beta create {}".format(name) if beta else "cloud create {}".format(name)