grace way to repeat a function

Peter Hansen peter at engcorp.com
Sun Jan 19 09:48:32 EST 2003


knightmare wrote:
> 
> i'm finding a way to repeat a function.used to do like this:
> for i in range(1,10):
>     print "function"
> but i think this not graceful.anyone have good idea ?

It's perfectly graceful, and more important, readable, although
there are two things you should do differently.

1. Make sure you really want it to repeat only 9 times... 
   otherwise use range(10) or, the same thing, range(0, 10)

2. Use xrange() instead of range(), in case you decide you
   want to iterate 1000000 times instead of ten times...

-Peter




More information about the Python-list mailing list