[Python-ideas] Variations on a loop
Chris Rebert
cvrebert at gmail.com
Thu Aug 28 07:34:00 CEST 2008
On Wed, Aug 27, 2008 at 10:07 PM, Bruce Leban <bruce at leapyear.org> wrote:
> There are several different blocks of code you could tack onto a loop (I've
> deliberately chosen somewhat unusual words to express these here):
>
> for x in items:
> # body
> interstitially:
> # things to do between loop iteration
> # (executed after each iteration in the loop when there is a next
> value)
> subsequently:
> # things to do after the last element of the loop is processed
> # (when the loop is not exited by break)
> contrariwise:
> # things to do if the list was empty
>
> For example:
>
> result = ""
> for x in items:
> result += str(x)
> interstitially:
> result += ", "
> contrariwise:
> result = "no data"
>
> When I first learned that Python had an 'else' clause on loops, I assumed it
> meant 'contrariwise'. I was surprised that it actually meant 'subsequently'.
>
> To be more clear, contrariwise is essentially equivalent to:
>
> empty = True
> for x in items:
> empty = False
> # body
> if empty:
> # do contrariwise code
>
> and interstitially is essentially equivalent to:
>
> first = True
> for x2 in items:
> if not first:
> # do interstitial code
> first = False
> x = x2
> # body
Just for comparison, (assuming my Perl6 book is still accurate),
"interstitially" is equivalent to "NEXT" blocks in Perl6 and
"continue" blocks in Perl5.
I don't think that's necessarily a good thing, but anyway...
- Chris
========
Follow the path of the Iguana...
Rebertia: http://rebertia.com
Blog: http://blog.rebertia.com
>
> I think these are common/useful paradigms. I'm curious what others think.
>
> --- Bruce
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
More information about the Python-ideas
mailing list