string issue

rbt rbt at athop1.ath.vt.edu
Fri Feb 4 15:25:04 EST 2005


John J. Lee wrote:
> Steve Holden <steve at holdenweb.com> writes:
> [...]
> 
>>You are modifying the list as you iterate over it. Instead, iterate
>>over a copy by using:
>>
>>for ip in ips[:]:
>>   ...
> 
> 
> Just to help popularise the alternative idiom, which IMO is
> significantly less cryptic (sane constructors of mutable objects
> almost always make a copy, and list is no exception: it's guaranteed
> to do so):
> 
> for ip in list(ips):
>    ...
> 
> 
> Works back to at least Python 1.5.2.
> 
> 
> John

I don't know that that approach is less cryptic. ips is already a 
list... it looks cryptic to make it a list again, doesn't it? IMO, the 
two are equally cryptic. The epitome of clarity would be copy(ips)... 
now *that* makes sense, of course, ips[:] or list(ips) work equally well 
to the programmer who has learned them.



More information about the Python-list mailing list