Re: Sets: elt in dict, lst.include - really begs for a PEP

On Tue, 30 Jan 2001, Guido van Rossum wrote:
Can you say "PEP time"? :-)
Okay, i have written a draft PEP that tries to combine the "elt in dict", custom iterator, and "for k:v" issues into a coherent proposal. Have a look: http://www.lfw.org/python/pep-iterators.txt http://www.lfw.org/python/pep-iterators.html Could i get a number for this please? -- ?!ng "The only `intuitive' interface is the nipple. After that, it's all learned." -- Bruce Ediger, on user interfaces

On Wed, 31 Jan 2001 01:47:10 -0800 (PST), Ka-Ping Yee <ping@lfw.org> wrote:
Er....one problem with first reading: you forgot to mention in the while loop description that 'else:' would be executed if the exception is raised, so the 'break' is a pseudo-break'. Basic response: I *love* the iter(), sq_iter and __iter__ parts. I tremble at seeing the rest. Why not add a method to dictionaries .iteritems() and do for (k, v) in dict.iteritems(): pass (dict.iteritems() would return an an iterator to the items) -- Moshe Zadka <sig@zadka.site.co.il> This is a signature anti-virus. Please stop the spread of signature viruses! Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

"MZ" == Moshe Zadka <moshez@zadka.site.co.il> writes:
MZ> Basic response: I *love* the iter(), sq_iter and __iter__ MZ> parts. I tremble at seeing the rest. Why not add a method to MZ> dictionaries .iteritems() and do | for (k, v) in dict.iteritems(): | pass MZ> (dict.iteritems() would return an an iterator to the items) Moshe, I had exactly the same reaction and exactly the same idea. I'm a strong -1 on introducing new syntax for this when new methods can handle it in a much more readable way (IMO). Another idea would be to allow the iterator() method to take an argument: for key in dict.iterator() a.k.a. for key in dict.iterator(KEYS) and also for value in dict.iterator(VALUES) for key, value in dict.iterator(ITEMS) One problem is that the constants KEYS, VALUES, and ITEMS would either have to be defined some place, or you'd just use values like 0, 1, 2, which is less readable perhaps than just having iteratoritems(), iteratorkeys(), and iteratorvalues() methods. Alternative spellings: itemsiter(), keysiter(), valsiter() itemsiterator(), keysiterator(), valuesiterator() iiterator(), kiterator(), viterator() ad-nauseum-ly y'rs, -Barry

"FL" == Fredrik Lundh <fredrik@pythonware.com> writes:
FL> shouldn't that be xitems, xkeys, xvalues? Or iitems(), ikeys(), ivalues()? Personally, I don't much care. If we get consensus on the more important issue of going with methods instead of new syntax, I'm sure Guido will pick whatever method names appeal to him most. -Barry

[Barry]
[/F]
shouldn't that be xitems, xkeys, xvalues?
I'm so hoping I missed a <wink> there somewhere. Please, no more of the dreaded 'x'. thinking-of-ripping-x-from-my-keyboard-ly y'rs, Z. -- Moshe Zadka <sig@zadka.site.co.il> This is a signature anti-virus. Please stop the spread of signature viruses! Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

[ Trimming CC: line ] On Wed, Jan 31, 2001 at 11:50:10AM -0500, Barry A. Warsaw wrote:
Same here. I *might* like it if iterators were given a format string (or tuple object, or whatever) so they knew what the iterating code expected (so something like this: for x,y,z in obj would translate into iterator(obj)("(x,y,z)") or maybe just iterator(obj)((None,None,None)) or maybe even just iterator(obj)(3) # that is, number of elements or so) but I suspect it might be too cute (and obfuscated) for Python, especially if it was put to use to distingish between 'for x:y in obj' and 'for x,y in obj'. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

barry@digicool.com (Barry A. Warsaw):
Yuck. I don't like any of this "for x in y.iterator_something()" stuff. The things you're after aren't "in" the iterator, they're "in" the dict. I don't want to know that there are iterators involved. We seem to be coming up with more and more convoluted ways to say things that should be very straightforward. Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+

Greg Ewing <greg@cosc.canterbury.ac.nz>:
I must say I agree. Having explicit iterators obfuscates what is going on, rather than clarifying it -- the details of how we get the next item should be hidden as far below the surface of the code as possible, so programmers don't have to think about them. The only cases I know of where an explicit iterator object is even semi-justified are those where there is substantial control state to be kept around between iterations and that state has to be visible to the application code (not the case with dictionaries or any other built-in type). In the cases where that *is* true (interruptible tree traversal being the paradigm example), we would be better served with Icon-style generators or a continuations facility a la Stackless Python. I'm a hard -1 on explicit iterator objects for built-in types. Let's keep it simple, guys. -- <a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a> The Constitution is not neutral. It was designed to take the government off the backs of the people. -- Justice William O. Douglas

Moshe Zadka wrote:
Barry Warsaw wrote:
I remember considering this solution when i was writing the PEP. The problem with it is that it isn't backward-compatible. It won't work on existing dictionary-like objects -- it just introduces another method that we then have to go back and implement on everything, which kind of defeats the point of the whole proposal. (One of the Big Ideas is to let the 'for' syntax mean "just do whatever you have to do to iterate" and we let it worry about the details.) The other problem with this is that it isn't feasible in practice unless 'for' can magically detect when the thing is a sequence and when it's an iterator. I don't see any obvious solution to this (aside from "instead of an iterator, implement a whole sequence-like object using the __getitem__ protocol" -- and then we'd be back to square one). I personally find this: for key:value in dict: much clearer than either of these: for (k, v) in dict.iteritems(): for key, value in dict.iterator(ITEMS): There's less to read and less punctuation in the first, and there's a natural parallel: seq = [1, 4, 7] for item in seq: ... dict = {2:3, 4:5} for key:value in dict: ... -- ?!ng Two links diverged in a Web, and i -- i took the one less travelled by. -- with apologies to Robert Frost

On Thu, 1 Feb 2001 03:31:33 -0800 (PST), Ka-Ping Yee <ping@lfw.org> wrote: [about for (k, v) in dict.iteritems(): ]
Well, in that case we have differing views on the point of the whole proposal. I won't argue -- I think all the opinions have been aired, and it should be pronounced upon.
dict.iteritems() could return not an iterator, but a magical object whose iterator is the requested iterator. Ditto itervalues(), iterkeys() -- Moshe Zadka <sig@zadka.site.co.il> This is a signature anti-virus. Please stop the spread of signature viruses! Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

"KY" == Ka-Ping Yee <ping@lfw.org> writes:
KY> Could i get a number for this please? Looks like you beat Eric to PEP 234. :) I'll update PEP 0 and let you check in your txt file. I may want to do an editorial pass over it. -Barry

On Wed, 31 Jan 2001 01:47:10 -0800 (PST), Ka-Ping Yee <ping@lfw.org> wrote:
Er....one problem with first reading: you forgot to mention in the while loop description that 'else:' would be executed if the exception is raised, so the 'break' is a pseudo-break'. Basic response: I *love* the iter(), sq_iter and __iter__ parts. I tremble at seeing the rest. Why not add a method to dictionaries .iteritems() and do for (k, v) in dict.iteritems(): pass (dict.iteritems() would return an an iterator to the items) -- Moshe Zadka <sig@zadka.site.co.il> This is a signature anti-virus. Please stop the spread of signature viruses! Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

"MZ" == Moshe Zadka <moshez@zadka.site.co.il> writes:
MZ> Basic response: I *love* the iter(), sq_iter and __iter__ MZ> parts. I tremble at seeing the rest. Why not add a method to MZ> dictionaries .iteritems() and do | for (k, v) in dict.iteritems(): | pass MZ> (dict.iteritems() would return an an iterator to the items) Moshe, I had exactly the same reaction and exactly the same idea. I'm a strong -1 on introducing new syntax for this when new methods can handle it in a much more readable way (IMO). Another idea would be to allow the iterator() method to take an argument: for key in dict.iterator() a.k.a. for key in dict.iterator(KEYS) and also for value in dict.iterator(VALUES) for key, value in dict.iterator(ITEMS) One problem is that the constants KEYS, VALUES, and ITEMS would either have to be defined some place, or you'd just use values like 0, 1, 2, which is less readable perhaps than just having iteratoritems(), iteratorkeys(), and iteratorvalues() methods. Alternative spellings: itemsiter(), keysiter(), valsiter() itemsiterator(), keysiterator(), valuesiterator() iiterator(), kiterator(), viterator() ad-nauseum-ly y'rs, -Barry

"FL" == Fredrik Lundh <fredrik@pythonware.com> writes:
FL> shouldn't that be xitems, xkeys, xvalues? Or iitems(), ikeys(), ivalues()? Personally, I don't much care. If we get consensus on the more important issue of going with methods instead of new syntax, I'm sure Guido will pick whatever method names appeal to him most. -Barry

[Barry]
[/F]
shouldn't that be xitems, xkeys, xvalues?
I'm so hoping I missed a <wink> there somewhere. Please, no more of the dreaded 'x'. thinking-of-ripping-x-from-my-keyboard-ly y'rs, Z. -- Moshe Zadka <sig@zadka.site.co.il> This is a signature anti-virus. Please stop the spread of signature viruses! Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

[ Trimming CC: line ] On Wed, Jan 31, 2001 at 11:50:10AM -0500, Barry A. Warsaw wrote:
Same here. I *might* like it if iterators were given a format string (or tuple object, or whatever) so they knew what the iterating code expected (so something like this: for x,y,z in obj would translate into iterator(obj)("(x,y,z)") or maybe just iterator(obj)((None,None,None)) or maybe even just iterator(obj)(3) # that is, number of elements or so) but I suspect it might be too cute (and obfuscated) for Python, especially if it was put to use to distingish between 'for x:y in obj' and 'for x,y in obj'. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!

barry@digicool.com (Barry A. Warsaw):
Yuck. I don't like any of this "for x in y.iterator_something()" stuff. The things you're after aren't "in" the iterator, they're "in" the dict. I don't want to know that there are iterators involved. We seem to be coming up with more and more convoluted ways to say things that should be very straightforward. Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+

Greg Ewing <greg@cosc.canterbury.ac.nz>:
I must say I agree. Having explicit iterators obfuscates what is going on, rather than clarifying it -- the details of how we get the next item should be hidden as far below the surface of the code as possible, so programmers don't have to think about them. The only cases I know of where an explicit iterator object is even semi-justified are those where there is substantial control state to be kept around between iterations and that state has to be visible to the application code (not the case with dictionaries or any other built-in type). In the cases where that *is* true (interruptible tree traversal being the paradigm example), we would be better served with Icon-style generators or a continuations facility a la Stackless Python. I'm a hard -1 on explicit iterator objects for built-in types. Let's keep it simple, guys. -- <a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a> The Constitution is not neutral. It was designed to take the government off the backs of the people. -- Justice William O. Douglas

Moshe Zadka wrote:
Barry Warsaw wrote:
I remember considering this solution when i was writing the PEP. The problem with it is that it isn't backward-compatible. It won't work on existing dictionary-like objects -- it just introduces another method that we then have to go back and implement on everything, which kind of defeats the point of the whole proposal. (One of the Big Ideas is to let the 'for' syntax mean "just do whatever you have to do to iterate" and we let it worry about the details.) The other problem with this is that it isn't feasible in practice unless 'for' can magically detect when the thing is a sequence and when it's an iterator. I don't see any obvious solution to this (aside from "instead of an iterator, implement a whole sequence-like object using the __getitem__ protocol" -- and then we'd be back to square one). I personally find this: for key:value in dict: much clearer than either of these: for (k, v) in dict.iteritems(): for key, value in dict.iterator(ITEMS): There's less to read and less punctuation in the first, and there's a natural parallel: seq = [1, 4, 7] for item in seq: ... dict = {2:3, 4:5} for key:value in dict: ... -- ?!ng Two links diverged in a Web, and i -- i took the one less travelled by. -- with apologies to Robert Frost

On Thu, 1 Feb 2001 03:31:33 -0800 (PST), Ka-Ping Yee <ping@lfw.org> wrote: [about for (k, v) in dict.iteritems(): ]
Well, in that case we have differing views on the point of the whole proposal. I won't argue -- I think all the opinions have been aired, and it should be pronounced upon.
dict.iteritems() could return not an iterator, but a magical object whose iterator is the requested iterator. Ditto itervalues(), iterkeys() -- Moshe Zadka <sig@zadka.site.co.il> This is a signature anti-virus. Please stop the spread of signature viruses! Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6

"KY" == Ka-Ping Yee <ping@lfw.org> writes:
KY> Could i get a number for this please? Looks like you beat Eric to PEP 234. :) I'll update PEP 0 and let you check in your txt file. I may want to do an editorial pass over it. -Barry
participants (7)
-
barry@digicool.com
-
Eric S. Raymond
-
Fredrik Lundh
-
Greg Ewing
-
Ka-Ping Yee
-
Moshe Zadka
-
Thomas Wouters