改善Python的if条件[关闭]

问题描述 投票:-3回答:3

如何提高下列条件,如果在Python 3.6到一条线。

def run_cmd(beta: bool): {
  cmd = "cloud create {}".format(self.name)
  if beta:
    cmd = "cloud beta create {}".format(self.name)
}
python if-statement format python-2.x code-cleanup
3个回答
1
投票

下面一个邮轮将实现它:

cmd = "cloud {}create {}".format("beta " if beta else "", name)

1
投票

一种方式来实现这一目标:

cmd = "cloud{} create {}".format(["", " beta"][beta], self.name)

1
投票

如果你想减少if语句:

name = ''
beta = True

cmd = "cloud beta create {}".format(name) if beta else "cloud create {}".format(name)
© www.soinside.com 2019 - 2024. All rights reserved.