Tuple slices
George Sakkis
gsakkis at rutgers.edu
Mon Jan 24 15:50:52 EST 2005
"Fredrik Lundh" <fredrik at pythonware.com> wrote in message
news:mailman.1214.1106591959.22381.python-list at python.org...
> Steven Bethard wrote:
>
> >>>>>a = 1, 2, 3
> >>>>>b = a[:]
> >>>>>a is b
> >> True
> >
> > My impression was that full tuple copies didn't actually copy, but that slicing a subset of a
> > tuple might. Not exactly sure how to test this, but:
> >
> > py> a = 1, 2, 3
> > py> a[:2] is a[:2]
> > False
>
> yup. and to figure out why things are done this way, consider this case:
>
> >>> a = give_me_a_huge_tuple()
> >>> len(a)
> (a rather large number)
> >>> b = a[:2]
> >>> del a
>
> (IIRC, I proposed to add "substrings" when I implemented the Unicode string
> type, but that idea was rejected, for the very same "and how do you get rid of
> the original object" reason)
>
> </F>
Fair enough. So perhaps the question is whether such cases are more regular than something like:
a = give_me_a_huge_tuple()
slices = [a[i:j] for i in xrange(len(a)) for j in xrange(i+1, len(a)+1)]
George
More information about the Python-list
mailing list