[Python-Dev] accumulator display syntax
Sean Ross
seandavidross at hotmail.com
Fri Oct 17 12:43:31 EDT 2003
Hi.
I've not posted to this group before, but I've been following most of the
discussions on it with interest for about 6 months. Yesterday I saw this
post:
Guido van Rossum wrote:
>I don't have a proposal for generator comprehension syntax though, and
>[yield ...] has the same problem.
I actually like the [yield ...] syntax, (I find the intent clear, and have
no expectations of its returning a list) but since that doesn't look like it
will be happening, I've tried to think of some other possible syntax. I've
come up with 16 different possibilities so far, including [yield ...], which
I've listed below. I'm not advocating any one of them (in fact, many of them
are abhorrent), I'm just listing some possibilities, in no particular order
other than as they occurred to me:
# (1) no delimiter
sumofsquares = sum(yield x*x for x in myList)
# (2) brackets
sumofsquares = sum([yield x*x for x in myList])
# (3) parentheses
sumofsquares = sum((yield x*x for x in myList))
# (4) braces
sumofsquares = sum({yield x*x for x in myList})
# (5) pipes
sumofsquares = sum(|yield x*x for x in myList|)
# (6) slashes
sumofsquares = sum(/yield x*x for x in myList/)
# (7) carets
sumofsquares = sum(^yield x*x for x in myList^)
# (8) angle brackets
sumofsquares = sum(<yield x*x for x in myList>)
# (9) sigil @
sumofsquares = sum(@yield x*x for x in myList@)
# (10) sigil $
sumofsquares = sum($yield x*x for x in myList$)
# (11) question marks
sumofsquares = sum(?yield x*x for x in myList?)
# (12) ellipses
sumofsquares = sum(...yield x*x for x in myList...)
# (13) yield:
sumofsquares = sum(yield:[x*x for x in myList])
# (14) unpacking (*)
sumofsquares = sum(*[x*x for x in myList])
# (15) <-
sumofsquares = sum(<-[x*x for x in myList])
#(16) ^
sumofsquares = sum(^[x*x for x in myList])
These last few suggestion (from (13) on) may require some explanation. The
notion I've had for "yield:" is to have it act something like a lambda so
that the list comprehension is not evaluated, i.e., no list is constructed
in memory. Instead, an iterator is created that can be used to generate the
items, one at a time, that would have been in that list. Something like
def squares(myList):
for x in myList:
yield x*x
sumofsquares = sum(squares(myList))
The other suggestions, after (13), are based on this same notion.
Okay. So, there are some generator comprehension syntax ideas. Hopefully
they will be useful, even if they just serve as items to point to and say
"we definitely don't want this".
I thank you for your time, and I apologize if these unsolicited suggestions
are unwanted.
Sean Ross
_________________________________________________________________
Add photos to your e-mail with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail
More information about the Python-Dev
mailing list