Stepping backwards in for loop?

Emile van Sebille emile at fenx.com
Sun Apr 15 12:11:29 EDT 2001


It's too bad there isn't a built-in string function like tuple and list.  I
do a fair amount of conversion from one to the other, and being able to
create functions that converted back to the source type might be helpful.
For example, suppose that type() returned the builtin function used to
create that type so that:

dir(type([])) returns ['__doc__', '__name__', '__func__' == <built-in
function list>]
dir(type(())) returns ['__doc__', '__name__', '__func__' == <built-in
function tuple>]
dir(type('')) returns ['__doc__', '__name__', '__func__' == <built-in
function string>]

Further suppose that function string inverted the tuple/list functionality
as they apply to strings.  Then it becomes possible to create a reverse
function that plays like:

>>> def reverseAny(s):
...  l = list(s)
...  l.reverse()
...  return type(s).__func__(l)

make-me-one-with-everything-ly y'rs

--

Emile van Sebille
emile at fenx.com

---------
"Steve Holden" <sholden at holdenweb.com> wrote in message
news:m5jC6.193253$m04.7292362 at e420r-atl1.usenetserver.com...
> "Andrew Dalke" <dalke at acm.org> wrote in message
> news:9banud$ge$1 at slb6.atl.mindspring.net...
> > Carlos Ribeiro wrote:
> > >It would be a *lot* easier if strings had a reverse method, or if the
> > >reverse() methods returned the reversed string. However, similarly to
> > >sort(), the Python-way-of-doing-things must have some good reason for
> > >reverse() to behave this way (as a inplace operation on the list).
> >
> > Lists have a reverse method because lists are mutable.
> > Consider strings as more akin to tuples.  Both are immutable.
> >
> But there's no good reason why there shouldn't be a "reverse" function
which
> returns the reverse of any sequence, surely? The question then becomes
> whether a built-in function could improve on the efficiency of
>
> >>> def sreverse(s):
> ...  l = list(s)
> ...  l.reverse()
> ...  return "".join(l)
> ...
> >>> sreverse("abcdefg")
> 'gfedcba'
>
> I'd be very surprised if it couldn't ... though then there's Unicode to
take
> into account.
>
> regards
>  STeve
>
>
>
>





More information about the Python-list mailing list