c[:]()
Duncan Booth
duncan.booth at invalid.invalid
Thu May 31 03:43:32 EDT 2007
irstas at gmail.com wrote:
> On May 31, 12:31 am, "Warren Stringer" <war... at muse.com> wrote:
>> This is inconsistent:
>>
>> why does c[:][0]() work but c[:]() does not?
>> Why does c[0]() has exactly the same results as c[:][0]() ?
>> Moreover, c[:][0]() implies that a slice was invoked
>
> It's not inconsistent, but [:] probably does something different than
> you think it does. All it does is create a copy (not in general, but
> at least if c is a list or a tuple). Since in your example c is a
> tuple and tuples are immutable, making a copy of it is essentially
> useless. Why not just use the original? I.e. instead of c[:] you could
> just write c. That's why c[:][0]() has exactly the same effect as c[0]
> (), although the former is likely to be slightly slower.
An extremely minor point (and implementation specific), but while [:] will
copy a list it won't copy a tuple. Likewise 'list(aList)' will always copy
a list, 'tuple(aTuple)' won't copy a tuple. So in this particular example
the slice is completely redundant.
>>> c is c[:] is c[:][:][:][:] is tuple(c)
True
More information about the Python-list
mailing list