Pythonic style
Terry Reedy
tjreedy at udel.edu
Mon Sep 21 09:39:15 EDT 2020
On 9/20/2020 6:34 PM, Stavros Macrakis wrote:
> I'm trying to improve my Python style.
>
> Consider a simple function which returns the first element of an iterable
> if it has exactly one element, and throws an exception otherwise. It should
> work even if the iterable doesn't terminate. I've written this function in
> multiple ways, all of which feel a bit clumsy.
The 'obvious' thing to me was the double try-except StopIteration. It
is clear, and clarity is 'pythonic'.
--
Terry Jan Reedy
More information about the Python-list
mailing list