Re: efficiency of range() and xrange() in for loops
In article <F4KdnXuGqOst1K nZnZ2dnUVZ_vudn Z2d@speakeasy.n et>,
Erik Max Francis <max@alcyone.co m> wrote:[color=blue]
>Alan Morgan wrote:
>[color=green]
>> In article <teXYf.69673$A8 3.1673324@twist er1.libero.it>,
>> Giovanni Bajo <noway@sorry.co m> wrote:
> >[color=darkred]
>>>Because you assume that the only use-case of range() is within a for-loop.
>>>range() is a builtin function that can be used in any Python expression. For
>>>instance:
>>>
>>>RED, GREEN, BLUE, WHITE, BLACK = range(5)[/color]
>>
>> Hmmm, this worked fine when I used xrange as well. Am I missing something?[/color]
>
>Not in your use case. Tuple unpacking will iterate, and so it doesn't
>matter whether it's an actual list or an iterator:
>[color=green][color=darkred]
> >>> a, b, c = xrange(3)
> >>> a[/color][/color]
>0[color=green][color=darkred]
> >>> b[/color][/color]
>1[color=green][color=darkred]
> >>> c[/color][/color]
>2
>
>There are certainly contexts where a sequence and its iterator are not
>interchangeabl e. You missed an obvious one:
>[color=green][color=darkred]
> >>> range(3) == xrange(3)[/color][/color]
>False[/color]
I thought that one was sufficiently obvious as not to need mentioning.
There was never any argument that range() and xrange() returned different
things; the question (as I understood it) was if you could generally
use the things they return in the same way and not *care* about the
difference (the answer being "Yes, except when you can't").
Alan
--
Defendit numerus
In article <F4KdnXuGqOst1K nZnZ2dnUVZ_vudn Z2d@speakeasy.n et>,
Erik Max Francis <max@alcyone.co m> wrote:[color=blue]
>Alan Morgan wrote:
>[color=green]
>> In article <teXYf.69673$A8 3.1673324@twist er1.libero.it>,
>> Giovanni Bajo <noway@sorry.co m> wrote:
> >[color=darkred]
>>>Because you assume that the only use-case of range() is within a for-loop.
>>>range() is a builtin function that can be used in any Python expression. For
>>>instance:
>>>
>>>RED, GREEN, BLUE, WHITE, BLACK = range(5)[/color]
>>
>> Hmmm, this worked fine when I used xrange as well. Am I missing something?[/color]
>
>Not in your use case. Tuple unpacking will iterate, and so it doesn't
>matter whether it's an actual list or an iterator:
>[color=green][color=darkred]
> >>> a, b, c = xrange(3)
> >>> a[/color][/color]
>0[color=green][color=darkred]
> >>> b[/color][/color]
>1[color=green][color=darkred]
> >>> c[/color][/color]
>2
>
>There are certainly contexts where a sequence and its iterator are not
>interchangeabl e. You missed an obvious one:
>[color=green][color=darkred]
> >>> range(3) == xrange(3)[/color][/color]
>False[/color]
I thought that one was sufficiently obvious as not to need mentioning.
There was never any argument that range() and xrange() returned different
things; the question (as I understood it) was if you could generally
use the things they return in the same way and not *care* about the
difference (the answer being "Yes, except when you can't").
Alan
--
Defendit numerus
Comment