4 ms·
Python follows convention the first convention, just as Dijkstra recommends. Mostly this worked-out well except for the case of descending sequences. With half
by raymondh 7y ago
Python follows convention the first convention, just as Dijkstra recommends. Mostly this worked-out well except for the case of descending sequences.
With half-open intervals, that case proved to be cryptic and error-prone, so we added reversed() to flip the direction of iteration. That allowed people to easily bridge from the comfortable and common case of looping forwards. Instead of range(n-1, -1, -1) we can write reversed(range(n)).
- mikepurvis 7y agoI was intrigued to see how this worked out in practice for Python 3, where range returns an iterator rather than a list— like, would reversing the iteration require allocating the whole list in memory? $ python3 -c 'print(reversed(range(10)))' <range_iterator object at 0x7fdb53ae96f0> It turns out that reversed() only works on objects that expose the __len__ and __getitem__ methods: https://www.python.org/dev/peps/pep-0322/ https://www.python.org/dev/peps/pep-0322/ If you try to use reversed() with your own generator it will fail with "TypeError: argument to reversed() must be a sequence", until you wrap the generator invocation in an explicit list(). Very nice.
- jacobolus 7y agoWith the new proxy feature, we can emulate Python 3 ranges in Javascript: https://observablehq.com/@jrus/itertools#range https://observablehq.com/@jrus/itertools#range
- thedirt0115 7y agoSome background on Python's choice for 0-based indexing: http://python-history.blogspot.com/2013/10/why-python-uses-0-based-indexing.html http://python-history.blogspot.com/2013/10/why-python-uses-0... See Guido's comment on calendar :)
- a1369209993 7y ago> Haven't you done enough damage? Well, clearly they (people who prefer 1-based indexing) don't consider the amount of damage they've done enough.