Re: [Python-Dev] {}.getitem() (was Re: PEP-0218)

Guido van Rossum <guido@python.org> writes:
I must admit I can't think of a situation where .firstkey would be more useful than .popkey; I thought that this discussion was discussion was inspired by the wish to write code like: while not set.empty(): x = set.popfirstkey() ... Maybe I'm jsut being dense, though. FWIW, I think dict.firstkey() reads better than dict.firstitem()[0] if the former's what you're actually trying to say. Why not write for k,_ in dict.items(): ... ? (Overstating the case slightly for effect, but I hope the point is clear). Cheers, M. -- Arrrrgh, the braindamage! It's not unlike the massively non-brilliant decision to use the period in abbreviations as well as a sentence terminator. Had these people no imagination at _all_? -- Erik Naggum, comp.lang.lisp

Michael Hudson wrote:
Just curious: how would you get at the deleted value when using .popfirstkey() ? But you have a point: dict.pop() returning the first filled slot in the dictionary as tuple (key,value) would probably be most effective. It's also nice and short. BTW, I don't get the inspiration for the "first" part in those names... it makes you think that there's an order to dictionary items when in reality there isn't.
No, the point is that you can modify the dictionary within the loop. You wouldn't see any changes using the second technique. -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Nice!
Actually there is -- the (arbitrary) order revealed by .keys(). --Guido van Rossum (home page: http://www.python.org/~guido/)

Guido van Rossum wrote:
Hmm, but this can change if you modify the dictionary inside the loop or if you modify the dictionary from some other thread. -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Let me explain. When Moshe first proposed a .key() method, I was a bit confused. I thought he wanted a way to get to the i-th key (which can't be done in O(1) time). Then I thought he wanted a way to get a random key. Finally I realized that all he wanted was *a* key, and the first key in the hash table would work fine. Since that's also the first key of .keys(), I figured "first" was an appropriate qualification. Sure, it's not guaranteed to remain the first key, but neither is the first item in a list. That said, getting rid of the first*() suite and adding popitem() instead sounds attractive... --Guido van Rossum (home page: http://www.python.org/~guido/)

Michael Hudson wrote:
Just curious: how would you get at the deleted value when using .popfirstkey() ? But you have a point: dict.pop() returning the first filled slot in the dictionary as tuple (key,value) would probably be most effective. It's also nice and short. BTW, I don't get the inspiration for the "first" part in those names... it makes you think that there's an order to dictionary items when in reality there isn't.
No, the point is that you can modify the dictionary within the loop. You wouldn't see any changes using the second technique. -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Nice!
Actually there is -- the (arbitrary) order revealed by .keys(). --Guido van Rossum (home page: http://www.python.org/~guido/)

Guido van Rossum wrote:
Hmm, but this can change if you modify the dictionary inside the loop or if you modify the dictionary from some other thread. -- Marc-Andre Lemburg ______________________________________________________________________ Company: http://www.egenix.com/ Consulting: http://www.lemburg.com/ Python Pages: http://www.lemburg.com/python/

Let me explain. When Moshe first proposed a .key() method, I was a bit confused. I thought he wanted a way to get to the i-th key (which can't be done in O(1) time). Then I thought he wanted a way to get a random key. Finally I realized that all he wanted was *a* key, and the first key in the hash table would work fine. Since that's also the first key of .keys(), I figured "first" was an appropriate qualification. Sure, it's not guaranteed to remain the first key, but neither is the first item in a list. That said, getting rid of the first*() suite and adding popitem() instead sounds attractive... --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (4)
-
Fredrik Lundh
-
Guido van Rossum
-
M.-A. Lemburg
-
Michael Hudson