Search code examples
pythonif-statementvariable-assignmentternary

Is there a better solution for this ternary condition?


Imagine the following ternary condition:

foreground = self.foreground if self.foreground else c4d.COLOR_TRANS

In this case, I need to call self.foreground twice just to check if it is True or not. Is there a way where I only need to call it once ?


Solution

  • An equivalent expression is

    foreground = self.foreground or c4d.COLOR_TRANS