On Wed, Jun 17, 2009 at 4:15 PM, Srijayanth Sridhar <srijaya...@gmail.com>wrote:
> > >> > As a python newbie, I find this a bit annoying. It would be nicer to have a > simple reverse method in the str class. > > name="The world according to Garp" > # name.reverse() or str.reverse(name) sure beats the hell out of > name[-1::-1] or name[::-1] > Unfortunately, this would violate the "immutability" of the str object. As you may know, a string object is immutable, i.e it cannot be modified in place. The 'reverse' as implemented on the list container modifies it in place. So a similar method on the 'str' container would also have to have a similar effect. But 'str' is immutable, so this is not possible. Otherwise 'reverse' on str has to return a new string object, reversed but then it violates the Python idiom that container types, when having similar methods, should work similarly. If you don't like s[::-1], then the closest would be ''.join([item for item in reversed(s)]), but that ain't close enough :) > > Jayanth > > > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > > -- -Anand
_______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers