I'm forced to maintain (or deploy) other people's Python code all the time. Personally, I would rarely choose it for a team project I have to maintain (if the choice is mine) because I've seen most teams commit the same annoying type errors over and over in every dynamic language I've ever used, including epecially Python. I've worked with much more competent Python programmers than myself who still do this.
But if I was on your team, I wouldn't complain and my code would be somewhere between pretty good to great. I would help the team ship that Python and ship it well.
I'm not sure what this rant says more about: me or this 'range vs xrange' question?
for i in range(n):
# your code here
because it potentially allocates a lot of memory for no reason whatsoever. It's the equivalent of this C code: int* r = calloc(sizeof(int), n);
for (int j = 0; j < n; j++) r[j] = j;
for (int j = 0; j < n; j++) {
int i = r[j];
// your code here
}
free(r);
If you don't want to deal with the distinction then what you can do is just treat range() as deprecated and always use either xrange() or list(xrange()) depending on the situation. But you had probably ought to know about the issue.And here's the thing: despite my rant, given that explanation, I'm going to have to say I respect that as a Python (2.x) interview question.
But that's because I care about performance optimization and good style in every language I use. I'm not sure that everyone else always should or that this should be a deal breaker for a python dev gig.
http://pythoncentral.io/how-to-use-pythons-xrange-and-range/
From that article (buried near the end for some reason):
>One more thing to add. In Python 3.x, the xrange function does not exist anymore. The range function now does what xrange does in Python 2.x, so to keep your code portable, you might want to stick to using range instead.
https://docs.python.org/3/whatsnew/3.0.html#views-and-iterat...
My Python is rusty, but I actually had to go look up the differences - my initial thought was "I'm pretty sure it used to be that range() generated a list and xrange() basically just iterated a value, but at some point range() was changed to use the xrange() behavior - I'd have to look it up to give you more detail than that." I didn't realize that xrange() had been completely removed from 3.x, for example. For that matter, I was thinking that both of them might have existed in 1.x, though it looks like they were added in 2.0.