Hacker News new | ask | show | jobs
by davidshepherd7 1553 days ago
Another problem with the current situation which isn't mentioned in the PEP: I often see people writing `foo_bar or 1` instead of `foo_bar if foo_bar is not None else 1`, which is usually incorrect when a is a number (zero is falsey). I think a terser "official" way of doing this would help there.

But, I often solve this with a function like:

``` def default_to(x: Optional[T], d: T) -> T: return x if x is not None else d ```

I wonder if having this function as the official way to do it would solve the problem without the "specialness" of `??`.

But there's no good equivalent that I know of for `?.`, that's a real pain to do with python as it is now so it seems like a nice addition.