string issue

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Mon Feb 7 12:12:09 EST 2005


Alex Martelli wrote:
> Bill Mill <bill.mill at gmail.com> wrote:
>    ...
>> > > You are modifying the list as you iterate over it. Instead, iterate over
>> > > a copy by using:
>> > >
>> > > for ip in ips[:]:
>    ...
>> Once you know it, it's neat, and I use it sometimes. However, it's a
>> little too "magical" for my tastes; I'd rather be more explicit about
>> what's going on.
> 
> Using ips[:] to make a copy on the fly is very idiomatic, but I've never
> liked it, personally.  I see no reason to prefer it to the even shorter
> and arguably less obscure ips*1, for example.

"Less obscure"? Either you know the `lst[:]' idiom, or you don't. If you
do, it's fine, if you don't, you will have to look it up to understand
its meaning, and that's a good thing.

Using `lst*1', on the other hand, does not make clear that a copy is
created. If you don't already know what it does, you may assume that
`lst' is left alone.

>  My preference is:
> 
>   for ip in list(ips):

That is the most intriguing variant, of course.

Reinhold



More information about the Python-list mailing list