variable assignment in "while" loop
Evan Simpson
evan at 4-am.com
Tue Jul 29 12:51:17 EDT 2003
More information about the Python-list mailing list
Tue Jul 29 12:51:17 EDT 2003
- Previous message (by thread): variable assignment in "while" loop
- Next message (by thread): variable assignment in "while" loop
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Sybren Stuvel wrote:
> while info = mydbcursor.fetchone():
> print "Information: "+str(info)
Gustavo has already pointed out the classic Pythonic ways of writing
this. In the general case, where you don't have an iterator handed to
you, you can make one as of Python 2.2 like so:
def each(f):
'''Make a zero-argument function or method iterable.
It had better have side effects, or this is pointless.'''
v = f()
while v:
yield v
v = f()
for info in each(mydbcursor.fetchone):
print "Information:", info
Of course, all this really does is to factor out one of the classic
Pythonic patterns into a wrapper function.
There's also the Pita pocket solution:
class Pita(object):
__slots__ = ('pocket',)
marker = object()
def __init__(self, v=marker):
if v is not self.marker:
self.pocket = v
def __call__(self, v=marker):
if v is not self.marker:
self.pocket = v
return self.pocket
p = Pita(10)
while p(p() - 1):
print p()
Cheers,
Evan @ 4-am
- Previous message (by thread): variable assignment in "while" loop
- Next message (by thread): variable assignment in "while" loop
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list