while (a=b()) ...

Tim Peters tim_one at email.msn.com
Sun May 16 17:53:50 EDT 1999


[Jim Meier]
> ...
> Aha! I see now... the * is intended to be the guarauneed-sequential
> number of the current iteration, then?

"for x in thing:" follows a protocol that's *defined* to call
thing.__getitem__ first with 0, then with 1, and so on.  That's how you can
write a class of your own whose instances participate in "for".  The 0, 1,
2, ... sequence is thus not arbitrary, but something the language
guarantees.

> I sympathise with the yearning, but my dislike is increased by the (to
> me, anyways) non-intuitive feel of it.

The sequence 0, 1, 2, ..., or the asterisk?

> Does  anyone use (untested):
> #defined somewhere global
> def iterate(seq):
>     return apply(lambda x,y: (seq(x), x), range(len(seq)))
>
> #where you need to iterate with index
> for (index,item) in iterate(sequence):
>     print "item %i: %s" % (index, sequence)
>
> ?

No, although it was suggested in each of the last 10 incarnations of this
thread too <wink>.  The usual spelling exploits a supernaturally fast
endcase of map:

def iterate(seq):
    return map(None, range(len(seq)), seq)

Believe Marc-Andre Lemburg has a version of this coded in C, available on
Starship as part of his tools extension module.  The irony here is that,
under the covers, map uses the sequence __getitem__ protocol to obtain the
elements of seq, generating 0, 1, 2, ... on its own, so that it can pair
your explicit construction of 0, 1, 2, ... with the sequence elements, all
so you can get at the 0, 1, 2, ... it later constructs *again* all on its
own in the eventual "for" loop <0.9 wink>.

Building an artificial and potentially huge list of pairs is unattractive
for obvious reasons.  The next suggestion is to make it lazy, etc etc.  I
think that all misses the point:  the index sequence is already part of the
language definition, and the runtime already generates it.  You simply can't
*name* it now.

> ...
> I was wondering if it was common to use a wrapper, both in this
> situation and in others.

It's very common to see wrappers suggested on c.l.py, for all sorts of
things.  I rarely see them used in actual code, though.  What you will see
is the simplest inline thing, in this case the "for i in xrange(len(seq)):"
business; e.g. last time I checked, somewhere around 15% of the "for" loops
in the std distribution were of this form, and none used a wrapper.

> ...
> Damned if you do, damned if you don't ... (Not really. Python is damn
> near perfect for what I like to use it for.)

Same here -- doesn't suppress the urge to tweak, though <wink>.

> ...
> However, my main, big, current interest in Python is it's readability
> and easy teachability to new users who have never programmed before.
> I'm trying to make programming a nearly integral part of using my
> project (a simulation-style game). So I'm looking for the most immediately
> readable solutions, so that the skittish-but-ready aren't scared off.

Will a simulation-style game have situations that require iterating over a
sequence terminated by a reserved sentinel value?  I don't think it's
coincidence that this thread always begins with reading lines from a huge
file.

then-again-it's-no-coincidence-that-my-toe-itches-either-ly y'rs  - tim






More information about the Python-list mailing list