Tuple slices

George Sakkis gsakkis at rutgers.edu
Mon Jan 24 17:42:55 EST 2005


"Peter Hansen" <peter at engcorp.com> wrote in message news:A9OdnRotCdqJ-2jcRVn-jA at powergate.ca...
> George Sakkis wrote:
> > 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)]
>
> I believe the general usage of tuples tends to mean that
> "give_me_a_huge_tuple()" just doesn't happen except with
> those who insist on using tuples as though they were nothing
> other than read-only lists.
>
> If you use a tuple the way it was apparently intended, you
> are extraordinarily unlikely to find yourself with a
> huge one requiring slicing in such a way that you care
> whether it is a "view" or a new object.
>
> -Peter

Actually my initial motivation was not a huge tuple I had to slice many times. It was something much
less extraordinarily unlikely, a recursive function with a sequence parameter:

def foo(sequence):
    # base_case
    # do_stuff()
    combine(foo(sequence[:n]),
                  foo(sequence[n:]))

Having each slice be a view of the original sequence instead of a fresh copy would be a Good Thing
(tm).

George





More information about the Python-list mailing list