python without OO
Nick Coghlan
ncoghlan at iinet.net.au
Thu Jan 27 05:15:22 EST 2005
beliavsky at aol.com wrote:
> Furthermore, if in Python the algorithm for the reverse function
> applies to many kinds of objects, it just needs to be coded once,
> whereas a reverse method would have to provided for each class that
> uses it (perhaps through inheritance).
Indeed, this is why Python not only provides the list.reverse() method to
reverse a list in place, but also provides the reversed() function to reverse
any sequence:
Py> lst = list("ABCDEFGHIJ")
Py> lst.reverse()
Py> print "".join(lst)
JIHGFEDCBA
Py> print "".join(reversed(lst))
ABCDEFGHIJ
Ditto list.sort() and sorted().
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-list
mailing list