Tuple slices
gsakkis at rutgers.edu
gsakkis at rutgers.edu
Tue Jan 25 10:14:11 EST 2005
Terry Reedy wrote:
> "George Sakkis" <gsakkis at rutgers.edu> wrote in message
> news:35lbvdF4k3ss4U1 at individual.net...
> > 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
>
> Why? To save time? memory? Either would require more that a few
bytes per
> slice. If they are, you can probably use virtual slices as follows.
>
> def foo(sequence):
> def _foo(seq, start, stop)
> # base_case
> # do_stuff()
> combine(_foo(seq, start, n), _foo(seq, n, stop))
> _foo(sequence, 0, len(sequence)
>
> In other words, if you don't really want slices copied out of the
sequence,
> then don't slice! Just use 2 ints to indicate the working region or
view.
> Both this and using a nested function with additional params are
standard
> techniques. This also works when the seq is mutable and you want
changes
> to the 'slice' to change the original, as in quicksort.
>
> Terry J. Reedy
I would say these are standard *C/C++* techniques; slicing simplifies
things and thus seems more 'pythonic' to me.
George
More information about the Python-list
mailing list