How does "for" work?

Steve Holden sholden at holdenweb.com
Wed Oct 11 18:12:54 EDT 2000


Steve Juranich wrote:
> 
> On Wed, 11 Oct 2000, Alex Martelli wrote:
> 
> > I see that other posts have already remarked that you need to
> > change the KeyError exception (returned by addressing the self.data
> > dictionary with a missing key) into an IndexError (which is what
> > a sequence returns, and for expects), but I'd like to understand
> > better what you're doing.  _Why_ is self.data a dictionary at all,
> > rather than a list?  Using it correctly with a for-loop requires
> > that the keys be a compact set of integers from 0 upwards -- and
> > in this case, what do you gain by making it a dictionary rather
> > than a list in the first place?
> >
> 
> self.data is a time-indexed bunch of data structures.  I would have used a
> list, but I wanted to allow for start times != 0.  I know that in the other
> "P" language, it allows re-defining the start index of a list.  Has Python
> implemented a similar bad idea?
> 

In that case, why not have a self.timeoffset attribute, set in the __init__
method or some other suitable place, which you subtract from each index
before accessing the list?  That way you get list speed and simplicity.

regards
 Steve
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/





More information about the Python-list mailing list