Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-13 Thread Steven D'Aprano
On Thu, Dec 13, 2018 at 06:53:54PM +1300, Greg Ewing wrote: > In any case, I don't claim that my MapView implements the full > iterator protocol, only enough of it to pass for an iterator in > most likely scenarios that assume one. Whether your hybrid sequence+iterator is close enough to an

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-13 Thread Paul Moore
On Thu, 13 Dec 2018 at 05:55, Greg Ewing wrote: > > Chris Angelico wrote: > > On Thu, Dec 13, 2018 at 3:07 PM Chris Barker - NOAA Federal via > > Python-ideas wrote: > > > > obj is iter(obj) > >> > >>Is that a hard and fast rule? > > Yes, it is. > > > >

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-12 Thread Chris Angelico
On Thu, Dec 13, 2018 at 4:54 PM Greg Ewing wrote: > > Chris Angelico wrote: > > On Thu, Dec 13, 2018 at 3:07 PM Chris Barker - NOAA Federal via > > Python-ideas wrote: > > > > obj is iter(obj) > >> > >>Is that a hard and fast rule? > > Yes, it is. > > > >

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-12 Thread Greg Ewing
Chris Angelico wrote: On Thu, Dec 13, 2018 at 3:07 PM Chris Barker - NOAA Federal via Python-ideas wrote: obj is iter(obj) Is that a hard and fast rule? Yes, it is. https://docs.python.org/3/library/stdtypes.html#iterator-types The docs aren't very clear on this point. They claim this

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-12 Thread Steven D'Aprano
On Wed, Dec 12, 2018 at 08:06:17PM -0800, Chris Barker - NOAA Federal wrote: > >>> and the test for an iterator is: > >>> > >>> obj is iter(obj) > > Is that a hard and fast rule? Yes, that's the rule for the iterator protocol. Any object can have an __iter__ method which returns anything you

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-12 Thread Chris Angelico
On Thu, Dec 13, 2018 at 3:07 PM Chris Barker - NOAA Federal via Python-ideas wrote: > > >>> and the test for an iterator is: > >>> > >>> obj is iter(obj) > > Is that a hard and fast rule? I know it’s the vast majority of cases, > but I imagine you could make an object that behaved exactly like

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-12 Thread Chris Barker - NOAA Federal via Python-ideas
>>> and the test for an iterator is: >>> >>> obj is iter(obj) Is that a hard and fast rule? I know it’s the vast majority of cases, but I imagine you could make an object that behaved exactly like an iterator, but returned some proxy object rather that itself. Not sure why one would do that,

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-12 Thread Steven D'Aprano
On Wed, Dec 12, 2018 at 12:50:41PM +1300, Greg Ewing wrote: > Steven D'Aprano wrote: > >The iterator protocol is that iterators must: > > > >- have a __next__ method; > >- have an __iter__ method which returns self; > > > >and the test for an iterator is: > > > >obj is iter(obj) > > By that

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Terry Reedy
On 12/11/2018 6:50 PM, Greg Ewing wrote: I'm not necessarily saying this *should* be done, just pointing out that it's a possible strategy for migrating map() from an iterator to a view, if we want to do that. Python has list and list_iterator, tuple and tuple_iterator, set and set_iterator,

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Steven D'Aprano
On Wed, Dec 12, 2018 at 11:31:03AM +1300, Greg Ewing wrote: > Steven D'Aprano wrote: > >I suggest we provide a separate mapview() type that offers only the lazy > >sequence API, without trying to be an iterator at the same time. > > Then we would be back to the bad old days of having two

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Greg Ewing
Steven D'Aprano wrote: The iterator protocol is that iterators must: - have a __next__ method; - have an __iter__ method which returns self; and the test for an iterator is: obj is iter(obj) By that test, it identifies as a sequence, as does testing it for the presence of __len__: >>>

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Chris Barker via Python-ideas
On Tue, Dec 11, 2018 at 11:10 AM Terry Reedy wrote: > > I _think_ someone may be advocating that map() could return an > > iterable if it is passed a iterable, > > I believe you mean 'iterator' rather than 'iterable' here and below as a > sequence is an iterable. > well, the iterator / iterable

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Greg Ewing
Steven D'Aprano wrote: I suggest we provide a separate mapview() type that offers only the lazy sequence API, without trying to be an iterator at the same time. Then we would be back to the bad old days of having two functions that do almost exactly the same thing. My suggestion was made in

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Terry Reedy
On 12/11/2018 12:01 PM, Chris Barker - NOAA Federal via Python-ideas wrote: Perhaps I got confused by the early part of this discussion. My point was that there is no “map-like” object at the Python level. (That is no Map abc). Py2’s map produced a sequence. Py3’s map produced an iterable. So

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Terry Reedy
On 12/11/2018 6:48 AM, E. Madison Bray wrote: The idea would be to now enhance the existing built-ins to restore at least some previously lost assumptions, at least in the relevant cases. To give an analogy, Python 3.0 replaced range() with (effectively) xrange(). This broken a lot of

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Terry Reedy
On 12/1/2018 8:07 PM, Greg Ewing wrote: Steven D'Aprano wrote: After defining a separate iterable mapview sequence class For backwards compatibilty reasons, we can't just make map() work like this, because that's a change in behaviour. Actually, I think it's possible to get the best of

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Chris Barker - NOAA Federal via Python-ideas
Perhaps I got confused by the early part of this discussion. My point was that there is no “map-like” object at the Python level. (That is no Map abc). Py2’s map produced a sequence. Py3’s map produced an iterable. So any API that was expecting a sequence could accept the result of a py2 map,

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Steven D'Aprano
On Tue, Dec 11, 2018 at 12:48:10PM +0100, E. Madison Bray wrote: > Right now I'm specifically responding to the sub-thread that Greg > started "Suggested MapView object", so I'm considering this a mostly > clean slate from the previous thread "__len__() for map()". Different > ideas have been

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Steven D'Aprano
On Mon, Dec 10, 2018 at 05:15:36PM -0800, Chris Barker via Python-ideas wrote: [...] > I'm still confused -- what's so wrong with: > > list(map(func, some_iterable)) > > if you need a sequence? You might need a sequence. Why do you think that has to be an *eager* sequence? I can think of two

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Paul Moore
On Tue, 11 Dec 2018 at 11:49, E. Madison Bray wrote: > The idea would be to now enhance the existing built-ins to restore at > least some previously lost assumptions, at least in the relevant > cases. To give an analogy, Python 3.0 replaced range() with > (effectively) xrange(). This broken a

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread E. Madison Bray
On Tue, Dec 11, 2018 at 12:13 PM Paul Moore wrote: > > On Tue, 11 Dec 2018 at 10:38, E. Madison Bray wrote: > > I don't understand why this is confusing. > [...] > > For something like a fixed sequence a "map" could just as easily be > > defined as a pair (, ) that applies , > > which I'm

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread Paul Moore
On Tue, 11 Dec 2018 at 10:38, E. Madison Bray wrote: > I don't understand why this is confusing. [...] > For something like a fixed sequence a "map" could just as easily be > defined as a pair (, ) that applies , > which I'm claiming is a pure function, to every element returned by > the . This

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-11 Thread E. Madison Bray
On Tue, Dec 11, 2018 at 2:16 AM Chris Barker wrote: > On Mon, Dec 10, 2018 at 5:23 AM E. Madison Bray wrote: >> >> Indeed; I believe it is very useful to have a map-like object that is >> effectively an augmented list/sequence. > > > but what IS a "map-like object" -- I'm trying to imagine what

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-10 Thread Chris Barker via Python-ideas
On Mon, Dec 10, 2018 at 5:23 AM E. Madison Bray wrote: > Indeed; I believe it is very useful to have a map-like object that is > effectively an augmented list/sequence. but what IS a "map-like object" -- I'm trying to imagine what that actually means. "map" takes a function and maps it onto a

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-10 Thread E. Madison Bray
On Sun, Dec 2, 2018 at 11:52 PM Greg Ewing wrote: > > Steven D'Aprano wrote: > > Perhaps more like the principle of most > > astonishment: the object changes from sized to unsized even if you don't > > modify its value or its type, but merely if you look at it the wrong > > way: > > Yes, but keep

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-02 Thread Greg Ewing
Steven D'Aprano wrote: Perhaps more like the principle of most astonishment: the object changes from sized to unsized even if you don't modify its value or its type, but merely if you look at it the wrong way: Yes, but keep in mind the purpose of the whole thing is to provide a sequence

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-02 Thread Steven D'Aprano
On Mon, Dec 03, 2018 at 02:04:31AM +1300, Greg Ewing wrote: > Chris Angelico wrote: > >I can't help thinking that it will be extremely surprising to have the > >length remain the same while the items get consumed. > > That can be fixed. The following version raises an exception if > you try to

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-02 Thread Greg Ewing
Chris Angelico wrote: I can't help thinking that it will be extremely surprising to have the length remain the same while the items get consumed. That can be fixed. The following version raises an exception if you try to find the length after having used it as an iterator. (I also fixed a bug

Re: [Python-ideas] Suggested MapView object (Re: __len__() for map())

2018-12-01 Thread Chris Angelico
On Sun, Dec 2, 2018 at 12:08 PM Greg Ewing wrote: > class MapView: > def __len__(self): > return min(map(len, self.args)) > > def __iter__(self): > return self > > def __next__(self): > if not self.iterator: > self.iterator = map(self.func,