define loop statement?

David Isaac aisaac0 at verizon.net
Sat Feb 18 17:56:46 EST 2006


> Alan Isaac wrote:
> > I would like to be able to define a loop statement
> > (nevermind why) so that I can write something like
> >
> > loop 10:
> >     do_something
> >
> > instead of
> >
> > for i in range(10):
> >     do_something
> >
> > Possible?  If so, how?


"Jeffrey Schwab" <jeff at schwabcenter.com> wrote in message
news:T4LJf.14256$915.10437 at southeast.rr.com...
> class Loop:
>  def __init__(self, n):
>   self.n = n
>  def __call__(self):
>   self.n = self.n - 1
>   return self.n != 0
>
>
> if __name__ == '__main__':
> loop = Loop(10)
> while loop():
>  print "OK"

OK, that's pretty good.
Thanks!
Alan Isaac





More information about the Python-list mailing list