semantics of [:]
Duncan Booth
duncan.booth at invalid.invalid
Fri Nov 20 15:20:49 EST 2009
Dave Angel <davea at ieee.org> wrote:
>
>
> Esmail wrote:
>> <div class="moz-text-flowed" style="font-family: -moz-fixed">Diez B.
>> Roggisch wrote:
>>> Esmail schrieb:
>>>> Could someone help confirm/clarify the semantics of the [:] operator
>>>> in Python?
>>>>
>>>> a = range(51,55)
>>>>
>>>> ############# 1 ##################
>>>> b = a[:] # b receives a copy of a, but they are independent
>>> >
>>>>
>>>> <snip>
>> <snip>
> (1) is most appropriate in that case. In fact, unless there is other
> code mixed in between, it's always the most appropriate.
An alternative view is that it is most appropriate to do:
b = list(a)
as that accepts any type of sequence for a and you don't have to remember
obscure punctuation to know what it does. The exception of course is when
(a) 'a' is sliceable and (b) you need to 'b' to be of the same type as 'a'.
More information about the Python-list
mailing list