How to improve the following if conditional in Python 3.6 to one line.
def run_cmd(beta: bool): {
cmd = "cloud create {}".format(self.name)
if beta:
cmd = "cloud beta create {}".format(self.name)
}
One way to achieve this:
cmd = "cloud{} create {}".format(["", " beta"][beta], self.name)
f"cloud {'beta ' if beta else ''}create {name}" in Python3 (since you prefer the A if cond else B construction).
ifstatement since you are settingbetatoFalseand there is nothing in your code that will change that.cmd = "cloud beta create {}".format(self.name) if beta else "cloud create {}".format(self.name)? That's assuming, of course, the value ofbetacould change.cmd = "cloud create "because beta is false and name is empty :) Bad questionbetawas not staticly fixed:cmd = "cloud {}create {}".format("beta " if beta else "", self.name)