New (?) suggestion re: 'while x = f(): ...'

jepler at unpythonic.net jepler at unpythonic.net
Thu May 30 08:38:20 EDT 2002


On Thu, May 30, 2002 at 08:19:22AM +0000, Duncan Booth wrote:
> Jeff Epler <jepler at unpythonic.net> wrote in 
> news:mailman.1022680666.24622.python-list at python.org:
> 
> >> 'H' is spelled 'iter' and already exists.
> > 
> > Not quite -- H will stop on "any false value", while iter(f, "") will
> > stop on a single value.
> 
> I think this is similar to the difference between except with a specific 
> exception, and except to catch any exception. The normal case should be to 
> know what the termination condition is. (Explicit is better than implicit.)

What do you write when f returns "an empty sequence" (but you don't care to
concern yourself whether it's tuple, list, UserList, string, ...)?

However, maybe the following class can be used in this kind of situation:
    class Z:
	def __eq__(self, other): return not other
    
>>> l = [1, 0, 2, 3, 4]
>>> list(iter(l.pop, Z()))
[4, 3, 2]

If only I was confident in the new set of rules for determining
(in)equality with the new methods...  I have the impression that as long as
the item returned from the function is NotImplemented for comparison with
instances of Z (and why would it be?), Z.__eq__ will be called each trip
through the loop.

Jeff





More information about the Python-list mailing list