Assignment in a while?
Alex Martelli
aleaxit at yahoo.com
Sun Apr 2 20:20:10 EDT 2006
none <""thulben\"@(none)"> wrote:
...
> can't do an assignment in the while loop. I found a way around this
> (see the commented out while loop), but it seems hackish. Assignment
> within a while loop seems like a pretty standard thing, so I'm just
> curious what I'm missing.
I see you've already received many excellent suggestions, and just
wanted to point out the way in which you CAN "assign-and-test" in those
rare occasions where you really want to (in my experience, that boils
down to: I need Python code whose structure is as close as possible to
some other's language -- either because I need to transliterate into
Python some published "reference implementation" kind of algorithm, or
because I know I'm just doing a _prototype_ in Python, and once that's
accepted some poor folks will have to transliterate it into C or
whatever). Anyway, it boils down to something like...:
class ValueHolder(object):
def __init__(self, value=None):
self.set(value)
def set(self, value):
self.value = value
return value
data = ValueHolder()
and then, say, something like...:
while data.set(zip.zop()):
frobnicate(data.value)
Not as Pythonic as iterators etc, but structurally very close to
while (xx=zip.zop()) {
frobnicate(xx);
}
if that's what you need to stick close to!-)
Alex
More information about the Python-list
mailing list