Coding style
Peter Otten
__peter__ at web.de
Tue Jul 18 02:28:19 EDT 2006
Carl Banks wrote:
> def process_values(lst):
> if not lst:
> return
> do_expensive_initialization_step()
> for item in lst:
> do_something_with(item)
> do_expensive_finalization_step()
> What if you called the function like this:
>
> process_values(x.strip() for x in values_lst)
>
> Oops, now we've just gone through an expensive initialization and
> finalization for nothing (since values_lst was empty). Maybe some
> subtle bugs introduced. If we're lucky, the finalization step will
> throw an exception.
The good news is that the above has a 99 percent chance that it just works
with iterators/generators -- even though the writer may not have been aware
of them/they didn't exist when the code was written...
Peter
More information about the Python-list
mailing list