Newbi Q: Recursively reverse lists but NOT strings?

Francesco Guerrieri f.guerrieri at gmail.com
Mon Oct 15 10:51:31 EDT 2007


On 10/15/07, Steven D'Aprano <steve at remove-this-cybersource.com.au> wrote:
> On Mon, 15 Oct 2007 14:47:30 +0200, Francesco Guerrieri wrote:
>
> >> def myreversed(sequence):
> >>     if isinstance(sequence, basestring):
> >>         return type(sequence)().join(reversed(sequence))
> >>     else:
> >>         return type(sequence)(reversed(sequence))
> >>
> >> (in fact, that's so simple I wonder why the built-in reversed() doesn't
> >> do that).
> >
> > simple:
> > In the face of ambiguity, refuse the temptation to guess.
>
> What ambiguity? What guess? The above is completely unambiguous: it
> returns a sequence of the same type as the input. It doesn't support
> xrange objects, but that's not really a great loss.

I could say that it depends on the xrange object.. but the truth is
that I answered too quickly. Your suggestion was not ambiguous. Sorry
:-)

francesco



More information about the Python-list mailing list