
Dec. 10, 2019
3:33 a.m.
Juancarlo Añez writes:
the_first_item_if_ordered = next(iter(container), default='not found')
Ouch!
one_item_if_any = first(return_a_set(), default=-1)
Is "first" really the right color for this bikeshed? Maybe it's OK, but speaking precisely you can't ask for "first" of a set. The question is "will 'any one' do?" There may be a natural (pre)order on the objects of a set, so that first_item_if_any = first(sorted(return_a_set(), default=-1)) is the desired result. I'm not sure if this is occurs more than very rarely in practice (but I know variations in iteration order for sets across invocations has bitten me in testing). It would be nasty to debug. Steve