list.reverse()
John Hunter
jdhunter at nitace.bsd.uchicago.edu
Fri Nov 15 21:29:37 EST 2002
>>>>> "Mike" == Mike Dean <klaatu at evertek.net> writes:
Mike> I think I recall seeing a reasonable explanation for this
Mike> earlier (probably referring actually to list.sort()), but
Mike> I'm wondering - what was the rationale for making
Mike> list.reverse() reverse a list in-place rather than return a
Mike> new, reversed list? I'm sure there's a good reason, but
Mike> what might that be?
Efficiency. You may not need a copy of both the list and the reversed
list.
This gives you the choice of convenience (add your own copy semantics)
or efficiency. If reverse always returned a copy, you'd be stuck with
convenience or writing your own in place reverse (which is harder than
writing your own copy)
class mylist(list):
def reversecopy(self):
new = self[:]
new.reverse()
return new
x = mylist([1,2,3,4])
y = x.reversecopy()
print x
print y
John Hunter
More information about the Python-list
mailing list