If init_x_value returns None , you can simply use:
return self._x or init_x_value() or self._x
Note that, for example, @ 3Doubloons says in its comment that if self._x is something false, like None , [] (empty list), () an empty tuple or a class that overrides the __bool__ method, it will go over to the init method, which can be computationally expensive.
In case init_x_value returns the computed value of x , you can even write it like this:
return self._x or init_x_value()
This is because or in Python is pretty special: x or y works like:
if bool(x): return x else: return y
and x and y generated lazily. Since bool(None) is False in the first case, it thus checks self._x . If self._x is None , it will be called by calling init_x_value() , since this method does not return anything that it implicitly returns None , which is not accepted by the or chain, so it finally resolves to the last self._x , which is now set.
source share