I avoid the syntax y if x else z when I can. This is inherently ugly, non-intuitive syntax and one of the big mistakes in Python design. This expression is out of order: x evaluates to y. It is not intuitive; it naturally reads as "if x, then y, else z". The C syntax gives us the decades-long established routine for this: x? y:z x? y:z . Python did this very wrong.
However, ternary syntax is the wrong mechanism for providing defaults. In self.maxTiles if self.maxTiles is not None else (2, 2) note the redundancy: you must specify self.maxTiles twice. This is repeated, so more work is required to read the code. I have to read it twice to make sure it does not say, for example, self.minTiles if self.maxTiles is not None else (2, 2) .
self.maxTiles or (0,2) avoids these problems; this is completely clear at a glance.
One warning: if self.maxTiles () either 0 or another false value, the result will be different. This is probably acceptable based on what you seem to be doing, but keep that in mind. This is a problem when providing a default value for a boolean or integer value, and you really need the is None test. For those whom I prefer a simple conditional, but sometimes it disappears from the triple expression.
Change A more understandable way to write a conditional version is:
if self.maxTiles is None: maxX, maxY = 2, 2 else: maxX, maxY = self.maxTiles
source share