Python 'for' loop is memory inefficient
nobody at nowhere.com
Mon Aug 17 05:10:17 CEST 2009
On Sun, 16 Aug 2009 11:41:21 -0400, Benjamin Kaplan wrote:
> It's not that the code is bad, but too many people coming from Java
> and C keep thinking of for loops like they're using Java or C and
> therefore that "for i in range(a,b)" is identical to "for(int i = a; i
> < b; i++)". It's not and, for the most part, you shouldn't code like
> that. Since you're using numbers, range/xrange is the appropriate
> idiom but you still have to remember that a for loop in python doesn't
> loop until a condition is met, it loops through an iterator until the
> interator says it's done.
Java also has iterators; it's more a case of people coming from C and BASIC.
Although, some of those may have come *through* Java without abandoning
old habits. You see the same thing with people coming from BASIC to C and
#define NUM_DATES 50
int day[NUM_DATES], month[NUM_DATES], year[NUM_DATES];
rather than defining a "struct".
Sometimes referred to as "I know ten languages and can write in BASIC in
all of them".
More information about the Python-list