c[:]()

irstas at gmail.com irstas at gmail.com
Wed May 30 17:59:16 EDT 2007


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.

c[:]() tries to call the copied tuple. Tuples aren't callable.
c[:][0]() calls the first element in the copied tuple, and that
element happens to be callable.




More information about the Python-list mailing list