AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
For loop with range python1/13/2024 ![]() ![]() It’s not that hard to use unt, or write your own infinite generator, so I don’t think the trade-offs for this proposal are worth it. ![]() And before anyone suggests it, I really don’t like the idea of range objects that might support Sequence operations, depending on the arguments used to create them… With infinite ranges, that’s no longer possible. Whether this is important in practice, is an open question, of course, but note that the following works with range objects at the moment: > range(10) On the other hand, it does support arbitrary step sizes.Īnd of course, the problem with the original proposal that range(start, None, step) should give an endless range, is precisely that this would mean that range objects could no longer be sequences either (from collections.abc, objects that implement Sequence must be sized and reversible, neither of which can be true for infinite ranges). and membership testing consumes values.More ways to do the same thing (perhaps deprecate unt?).Do some processing lst ind processedelem. The are cases when it's useful to have the index around, though, such as when you need to assign back to the list: for ind in range (len (lst)): elem lst ind. len of a range is no longer always nice For such simple case, for ind in range (len (sequence)) is generally considered an anti-pattern.Performance hit (probably tiny) on finite loops.I would also suggest range() should default to an end of None with zero arguments: for i in range(): The step and start parameters could be omitted as normal. there is no ‘stop’ and the iterable goes on forever. This makes sense, as the ‘stop’ parameter is None, i.e. I propose using range(start, None, step) for an endless range. In these cases, having to import itertools to get an infinite loop makes the program significantly longer. Secondly, infinite loops like this are quite common in tiny example programs, e.g. In some other languages the finite and infinite cases look very similar, C (and other languages with the same loop syntax) and Rust being two examples. Without knowing, it’s easy to assume that unt does a similar thing to unt or str.count, rather than range. Firstly, unt is doing a very similar thing to range, yet seems on first glance completely unrelated. One can replace the ‘10’ with a long sequence of nines, write a generator function or just switch to a while loop, but the current ‘best’ way is using itertools: import itertools Currently, to iterate over finite arithmetic sequences of integers, range is used, as in: for i in range(10):įor an infinite arithmetic sequence, there are a few approaches.
0 Comments
Read More
Leave a Reply. |