Looking under Python's hood: Will we find a high performance or clunky engine?

88888 Dihedral dihedral88888 at googlemail.com
Mon Jan 23 13:56:49 EST 2012


在 2012年1月23日星期一UTC+8上午2时01分11秒,Robert Kern写道:
> On 1/22/12 3:50 PM, Rick Johnson wrote:
> >
> > What does Python do when presented with this code?
> >
> > py>  [line.strip('\n') for line in f.readlines()]
> >
> > If Python reads all the file lines first and THEN iterates AGAIN to do
> > the strip; we are driving a Fred flintstone mobile. If however Python
> > strips each line of the lines passed into readlines in one fell swoop,
> > we made the correct choice.
> >
> > Which is it Pythonistas? Which is it?
> 
> The .readlines() method is an old API that predates the introduction of 
> iterators to Python. The modern way to do this in one iteration is to use the 
> file object as an iterator:
> 
>    [line.strip('\n') for line in f]

This is more powerful by turning an object to be iterable.

But the list comprehension violates the basic operating 
principle of the iteratee chaining rule in programming.
 
I know manny python programmers just abandon the list comprehension
in non-trivial processes. 




More information about the Python-list mailing list