On Tue, Mar 22, 2016 at 11:07 PM, Sven R. Kunze
However, I assume that when I do "object()" I got something unique among all other objects. So,
item = sentinel = object() for item in collection: main_suite(item) if item is sentinel: empty_suite()
Is still quite correct, right?
Sure it is. Perfectly correct.
def nope(): ... item = sentinel = object() ... for item in locals().values(): ... print("We have:", item) ... if item is sentinel: ... print("We have no items.") ... nope() We have:
An iterator can return *any* *object*. That's why Python uses an exception (StopIteration) to signal the absence of an object. More reliable is to use that absence, either by exception or by probing a dictionary's keys: def better(): # Make sure item is unbound try: del item except NameError: pass for item in locals().values(): print("We have:", item) if 'item' not in locals(): print("We have no items.") But now we're getting into the realm of ugly code to deal with edge cases. Like with "yield from", language support can be justified when there's a simple and obvious *but imperfect* way to do something ("yield from x" is not the same as "for item in x: yield item"). ChrisA