[Python-Dev] PEP 0424: A method for exposing a length hint (original) (raw)
Christian Heimes lists at cheimes.de
Sun Jul 15 16:56:39 CEST 2012
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Am 15.07.2012 16:39, schrieb Mark Shannon:
1. Don't implement it at all.
2. Implement lengthhint() 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
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]