<br><br><div class="gmail_quote">On Sat, Jul 14, 2012 at 4:18 PM, Benjamin Peterson <span dir="ltr"><<a href="mailto:benjamin@python.org" target="_blank">benjamin@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
2012/7/14 Alex Gaynor <<a href="mailto:alex.gaynor@gmail.com">alex.gaynor@gmail.com</a>>:<br>
><br>
> Proposal<br>
> ========<br>
><br>
> This PEP proposes formally documenting ``__length_hint__`` for other<br>
> interpreter and non-standard library Python to implement.<br>
><br>
> ``__length_hint__`` must return an integer, and is not required to be accurate.<br>
> It may return a value that is either larger or smaller than the actual size of<br>
> the container. It may raise a ``TypeError`` if a specific instance cannot have<br>
> its length estimated. It may not return a negative value.<br>
<br>
And what happens if you return a negative value?<br>
<br></blockquote><div><br></div><div>ValueError, the same as with len.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
><br>
> Rationale<br>
> =========<br>
><br>
> Being able to pre-allocate lists based on the expected size, as estimated by<br>
> ``__length_hint__``, can be a significant optimization. CPython has been<br>
> observed to run some code faster than PyPy, purely because of this optimization<br>
> being present.<br>
><br>
> Open questions<br>
> ==============<br>
><br>
> There are two open questions for this PEP:<br>
><br>
> * Should ``list`` expose a kwarg in it's constructor for supplying a length<br>
> hint.<br>
> * Should a function be added either to ``builtins`` or some other module which<br>
> calls ``__length_hint__``, like ``builtins.len`` calls ``__len__``.<br>
<br>
Let's try to keep this as limited as possible for a public API.<br>
<span class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div>Sounds reasonable to me! Should we just go ahead and strip those out now?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class="HOEnZb"><font color="#888888">
<br>
--<br>
Regards,<br>
Benjamin<br>
</font></span></blockquote></div><br>Alex<br clear="all"><div><br></div>-- <br>"I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)<br>
"The people's good is the highest law." -- Cicero<br><br>