[Python-ideas] Invalidate list iterators after size changes?

2021-10-09 Thread Tyler Hou via Python-ideas
Right now, the following code is valid in Python 3.9 (and infinitely loops): ``` lst = [1, 2, 3] for i in lst: lst.append(i) ``` IMO this is a source of bugs, and even though this behavior is technically documented (https://bugs.python.org/issue32767), I don't know if it should belong in th

[Python-ideas] Re: Invalidate list iterators after size changes?

2021-10-09 Thread Serhiy Storchaka
09.10.21 01:24, Tyler Hou via Python-ideas пише: > Concretely, I'm proposing the following set of changes to Python: > > 1. If the size of a list, set, or dict changes, invalidate all existing > iterators to that containers. Document this behavior as part of the public > API. > 2. Attempting to

[Python-ideas] Re: dict_items.__getitem__?

2021-10-09 Thread Stephen J. Turnbull
Christopher Barker writes: > But last time, one of the use cases was "get [an arbitrary] item > from a dict", and there really is not a terribly easy (and > efficient) way to do that now. What's wrong with thedict.popitem()? Works in Python 2.7, BTW. Granted, this doesn't help for a random i

[Python-ideas] Re: Invalidate list iterators after size changes?

2021-10-09 Thread Spencer Brown
Deleting is quite error prone, but appending during iteration is very useful in implementing an iterative form of recursive algorithms. It effectively gives a FIFO queue which remembers all items after you're done. In general the behaviour of a list iterator can be understood completely if you e

[Python-ideas] Re: dict_items.__getitem__?

2021-10-09 Thread Steven D'Aprano
On Sun, Oct 10, 2021 at 01:51:52AM +0900, Stephen J. Turnbull wrote: > Christopher Barker writes: > > > But last time, one of the use cases was "get [an arbitrary] item > > from a dict", and there really is not a terribly easy (and > > efficient) way to do that now. > > What's wrong with thedi

[Python-ideas] Re: Invalidate list iterators after size changes?

2021-10-09 Thread Greg Ewing
On 9/10/21 11:24 am, Tyler Hou via Python-ideas wrote: Right now, the following code is valid in Python 3.9 (and infinitely loops): ``` lst = [1, 2, 3] for i in lst: lst.append(i) ``` 1. If the size of a list, set, or dict changes, invalidate all existing iterators to that containers. T

[Python-ideas] Re: Invalidate list iterators after size changes?

2021-10-09 Thread Paul Bryan
Is this behavior of list iteration guaranteed by documentation anywhere? A quick search didn't yield any results for me. If not, I suggest either: a) it gets documented so developers can rely on it, or b) the lack of guarantee is documented so that we can reserve a change of behavior later. Paul

[Python-ideas] Adding a Version type

2021-10-09 Thread Finn Mason
Hello all, I was thinking about the following idioms: __version__ = "1.0" import sys if sys.version_info < (3, 6): # Yell at the user I feel like there could be a better way to define versions. There's no real standard way to specify versions in Python, other than "use semantic versionin

[Python-ideas] Random item from a dict [was Re: Re: dict_items.__getitem__?]

2021-10-09 Thread Steven D'Aprano
On Fri, Oct 08, 2021 at 01:42:35PM -0700, Christopher Barker wrote: > It really ius frustrating how often we repeat entire conversations on this > list :-( That's because y'all don't just admit I'm right the first time *wink* > But last time, one of the use cases was "get a random item from a d

[Python-ideas] Re: Invalidate list iterators after size changes?

2021-10-09 Thread David Mertz, Ph.D.
I've known and relied on the behavior since Python 1.4. I mean, not often, but it's been consistent. In the case where I want to loop over what the loop was at start, rather than what it might become, a slice comes in handy: for item in mylist[:]: # do stuff, maybe mutate list On Sat, Oct 9

[Python-ideas] Re: Random item from a dict [was Re: Re: dict_items.__getitem__?]

2021-10-09 Thread David Mertz, Ph.D.
On Sat, Oct 9, 2021, 10:44 PM Steven D'Aprano > Apart from a programming exercise and teaching moment, why would you want > to get a random key and value from a dict? In 25-ish years of using Python, > I think that the number of times I've needed to do that is zero. A random > item from a list, do

[Python-ideas] Re: Adding a Version type

2021-10-09 Thread Christopher Barker
There is a Version type in the packaging package that imjments the “official” versioning scheme for PyPI packages. https://packaging.pypa.io/en/latest/index.html# Should it be in the stdlib? Maybe, I think it has used casss beyond pip and friends. I’ve actually written my own more limited vers

[Python-ideas] Re: Random item from a dict [was Re: Re: dict_items.__getitem__?]

2021-10-09 Thread Christopher Barker
As was discussed the last time, I don’t know that selecting a random item from a dict has a lot of use cases — I’m pretty sure the only time I’ve needed to do it was for Dave Thomas’s “trigrams” coding kata. Though I’m not sure I’ve used random.choice fir anything mor “real” either. But it is a mo

[Python-ideas] Re: dict_items.__getitem__?

2021-10-09 Thread Steven D'Aprano
On Fri, Oct 08, 2021 at 01:42:35PM -0700, Christopher Barker wrote: > Anyway, I do see the benefit of adding first() to itertools -- there really > is a key problem with: > > next(iter(an_iterable)) > > for newbies -- you can get really really far in Python without ever having > to call either

[Python-ideas] Re: dict_items.__getitem__?

2021-10-09 Thread Finn Mason
On Sat, Oct 9, 2021, 9:56 PM Steven D'Aprano wrote: > [Snip...] Newbies won't know first() lives in itertools, and those experienced > enough to know it is there probably won't bother to use it. > A very good point. Let's get back to the original topic. Should `dict.items()` be indexable now

[Python-ideas] Re: dict_items.__getitem__?

2021-10-09 Thread Steven D'Aprano
On Wed, Oct 06, 2021 at 03:42:28PM +0100, Alex Waygood wrote: > > Whether they are added to dict or itertools, there are still nine of > > them > > No, the suggestion was to add two functions to itertools (first() and > last(), which would work with any iterable, not just dicts), rather > than

[Python-ideas] Re: dict_items.__getitem__?

2021-10-09 Thread Chris Angelico
On Sun, Oct 10, 2021 at 3:05 PM Finn Mason wrote: > > On Sat, Oct 9, 2021, 9:56 PM Steven D'Aprano wrote: >> >> [Snip...] > > >> Newbies won't know first() lives in itertools, and those experienced >> enough to know it is there probably won't bother to use it. > > > A very good point. > > Let's g