Niklas R
Niklas R

Reputation: 16860

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 ?

Upvotes: 3

Views: 109

Answers (2)

GaretJax
GaretJax

Reputation: 7780

You can use the boolean operators:

foreground = self.foreground or c4d.COLOR_TRANS

Upvotes: 3

Sven Marnach
Sven Marnach

Reputation: 601461

An equivalent expression is

foreground = self.foreground or c4d.COLOR_TRANS

Upvotes: 7

Related Questions