Am 15.07.2012 16:39, schrieb Mark Shannon: > 1. Don't implement it at all. > > 2. Implement __length_hint__() but don't want to return any value. > Either raise an exception (TypeError) -- As suggested in the PEP. > or return NotImplemented -- my preferred option.
How is this different from "don't know"? What's the use case for knowing that the object doesn't want to say anything or doesn't know its possible length. > 3. Return a "don't know" value: > Returning 0 would be fine for this, but the VM might want to respond > differently to "don't know" and 0. How about None? It's the logical choice, simple and easy to test for in Python and C code. 0 is a valid number for "I know that's I'll return nothing". > 4. Infinite iterator: > Could return float('inf'), but given this is a "hint" then > returning sys.maxsize or sys.maxsize + 1 might be OK. > Alternatively raise an OverflowError Too complex, hard to remember and even harder to check for. Since a length is always positive or zero, -1 is a good return value for infinite. > a) Don't want to return any value or "don't know": return NotImplemented +1 > b) For infinite iterators: raise an OverflowError -1, I'm for -1. ;) I'm not a fan of using exception for valid and correct return values. > c) All other cases: return an int or a type with a __index__() method. +1 Christian _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com