News for hackers. Stuff that matters.
Re:WTFPython: Exploring and understanding Python through sur... by dragonwriter on Tuesday May 31, 2022 @02:00PM (#31572718)
Honestly, for this specific case, I prefer one_or_none() to return an iterable with zero or one items, and then just doing:

  for foo in one_or_none():
    do_stuff(foo)
If you don't control one_or_none, but it returns an Optional, you can wrap it with something like:

  def optional_to_tuple(opt_val: Optional[T]) -> Tuple[]|Tuple[T]:
    return (opt_val,) if opt_val is not None else ()

There are no comments on this comment yet.