Newbi Q: Recursively reverse lists but NOT strings?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Oct 15 10:14:31 EDT 2007
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.
--
Steven.
More information about the Python-list
mailing list