stdazi wrote: > Hello! > > Many times I was suggested to use xrange and range instead of the > while constructs, and indeed, they are quite more elegant - but, after > calculating the overhead (and losen flexibility) when working with > range/xrange, and while loops, you get to the conclusion that it isn't > really worth using range/xrange loops. > > I'd like to show some examples and I'll be glad if someone can suggest > some other fixes than while a loop :-) > > a) range overfllow : > > > for i in range(0, 1 << len(S)) : > ..... > OverflowError: range() result has too many items > > ok, so we fix this one with xrange ! > > b) xrange long int overflow : > > for i in xrange(0, 1 << len(S)) : > ........ > OverflowError: long int too large to convert to int > > Next thing I miss is the flexibility as in C for loops : > > for (i = 0; some_function() /* or other condition */ ; i++) > > or, > > for (i = 0 ; i < 10 ; i++) > i = 10; > > > I don't think range/xrange sucks, but I really think there should be > some other constructs to improve the looping flexibility. Other thing > may be, that I just miss an equally elegant alternative that's why I'd > like to hear some suggestions on how to fix the above issues.. (btw, > I've already browsed the archives related to my issue,but i don't see > any good solution) > > Thanks > > Jernej. >
Your example of for i in xrange(0, 1<<len(s)): must have resulted in a number greater than 1 billion. Are you really doing this much or are you just pointing out an edge case here? You can always use while loop: i=0 while i < (1<<len(s)): i+=1 or i=1 while 1: if i == 10: break i+=1 Personally I use a lot of for loops in my code where I iterate over lists or with a generator as my target that returns instances of my data until exhausted and find the code easy to read: for line in myfile: # do something with the line No need to even "think" about numbers here unless I want to keep track of the line numbers in which case I would do: for linenumber, line in enumerate(myfile): # do something with each linenumber, line I find that I use i, j, k pointers less and less as I write more code in Python. I guess it is just what you get accustomed to using. -Larry -- http://mail.python.org/mailman/listinfo/python-list