[BangPypers] which is better solution of the question

Anand Balachandran Pillai abpillai at gmail.com
Wed Jun 17 15:36:48 CEST 2009


On Wed, Jun 17, 2009 at 4:15 PM, Srijayanth Sridhar <srijayanth at 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 at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>


-- 
-Anand
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20090617/f34a78e9/attachment.htm>


More information about the BangPypers mailing list