Re: [Python-ideas] Consider making enumerate a sequence if its argument is a sequence
On Wed, 30 Sep 2015 at 09:38 Neil Girdhar
In fairness, one is a superset of the other. You always get an Iterable. You sometimes get a Sequence. It's a bit like multiplication? with integers you get integers, with floats, you get floats.
No, it's not like multiplication. =) I hate saying this since I think it's tossed around too much, but int/float substitution doesn't lead to a Liskov substitution violation like substituting out a sequence for an iterator (which is what will happen if the type of the argument to `enumerate` changes). And since you can just call `list` or `tuple` on enumerate and get exactly what you're after without potential bugs cropping up if you don't realize from afar you're affecting an assumption someone made, I'm -1000 on this idea. -Brett
On Wed, Sep 30, 2015 at 12:35 PM Brett Cannon
wrote: On Wed, 30 Sep 2015 at 08:28 Neil Girdhar
wrote: What are the pros and cons of making enumerate a sequence if its argument is a sequence?
I found myself writing:
for vertex, height in zip( self.cache.height_to_vertex[height_slice], range(height_slice.start, height_slice.stop)):
I would have preferred:
for height, vertex in enumerate( self.cache.height_to_vertex)[height_slice]:
Because you now suddenly have different types and semantics of what enumerate() returns based on its argument which is easy to mess up if self.cache.height_to_vertex became an iterator object itself instead of a sequence object. It's also not hard to simply do `tuple(enumerate(...))` to get the exact semantics you want: TOOWTDI.
IOW all I see are cons. =)
participants (17)
-
Akira Li
-
Alexander Belopolsky
-
Andrew Barnert
-
Brendan Barnwell
-
Brett Cannon
-
Chris Angelico
-
Chris Barker
-
Emile van Sebille
-
M.-A. Lemburg
-
Neil Girdhar
-
Paul Moore
-
Random832
-
Serhiy Storchaka
-
Stephen J. Turnbull
-
Steven D'Aprano
-
Sven R. Kunze
-
Terry Reedy