[Python-ideas] Length hinting and preallocation for container types

Eli Bendersky eliben at gmail.com
Tue Mar 5 20:44:15 CET 2013


> >    The real problem is that this code is not idiomatic Python,
> especially if
> >    you want it to be reasonably fast:
> >
> >    � lst = []
> >    � for i in range(100):
> >    � � � lst.append(i*i)
> >
> >    Why not:
> >
> >    lst = [i*i for i in range(100)]
> >
> >    If the "append" pattern is complex, just "preallocate" like this:
> >
> >    lst = [0] * 100
> >
> >    And then fill it.
> >
> >    Eli
>


> >How would you replicate the behavior of __exit__ in Christian's example?
>
>
Why would I want to replicate it?

Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130305/6d4ea428/attachment.html>


More information about the Python-ideas mailing list