[Python-3000] Iterators for dict keys, values, and items == annoying :)

Adam DePrince adam.deprince at gmail.com
Mon Mar 27 06:08:04 CEST 2006


I have a draft PEP and an implementation of mutable iterators for lists
and dicts that supports delete only.  

The PEP (Mutable Iterations) and sample code can be found at:

http://www.deprince.net/ideas/peps.html

Or, you can get the patch from:

http://sourceforge.net/tracker/index.php?func=detail&aid=1459011&group_id=5470&atid=305470

There are two questions that I'd like to put before the Python community
at large.   The first is which of Java's iterator methods we would like
to see.   

I'm using Java's list-iterator as a template - 

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ListIterator.html

Question #1: Pick which you like, and which you think are too
non-pythonic.  Let me know.  At a minimum we need delete, I'd like to
see others, but I'd like to get some feedback before adding something
that would make everyone recoil.  

Operation      Works with list      Works with dict

add            Yes                  Unreliably**
hasNext        Yes                  Yes
hasPrev        Yes                  Yes
previous       Yes                  Yes

next           Already implemented 
remove         Implemented with patch, called delete (see question #3)
set            Yes                  Yes only for dict.values()
previousIndex  Yes                  Yes
nextIndex      Yes                  Yes 

And not part of Java, but I'd like to ask everyone anyway.

current        Yes                  Yes    
(the item last returned by next/prev)
currentIndex   Yes                  Yes
(the item index last returned by next/prev) 

Question #2: 

What should delete() return?   I currently have it returning the iter
itself to make it possible to say:

value = iter.delete().next()

Example:
item = i.next()
while reject( item ):
	item = i.delete().next()

I fully expect a groundswell of people to say "just return None."  

Question #3:

Delete vs. remove.  On the first cut I took some liberties with the
naming.  A lot of data-structures have a remove method, the semantics
are datastructre.remove( offending_item ).  Because in the iterator,
which item is selected by the iterator state, not parameter, I didn't
want to confuse the two, so I called it remove.   

The idea is remove has to be told what, delete refers to its internal
state. 

Question #4:

At first I wanted to implement Java fast-fail semantics, but now I have
reservations.  It would require a bit of house keeping on each and evert
insert.  IMHO, even adding as little as "flag=1" is too much for what is
likely one of the most commonly used pieces of code in Python.  Can I
have a show of hands?  For?  against?  Wait and see how bad the
performance hit is before deciding? 

---

** Python dicts never rehash on delete, but they can and sometimes must
on insert.  In fact, you often *can't* do more than fixed number of
inserts without a mandatory rehash, otherwise you just run out slots.  

Even if somebody decides that delete should rehash, iters don't use the
dict's delete ... why delete by key when we have something better, the
slot #?  A little housekeeping and the item is gone with no searching
and no comparisons.

Cheers - Adam DePrince



More information about the Python-3000 mailing list