[Python-Dev] PEP 0424: A method for exposing a length hint (original) (raw)
Alexandre Zani alexandre.zani at gmail.com
Sun Jul 15 01:28:07 CEST 2012
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Sat, Jul 14, 2012 at 4:21 PM, Alex Gaynor <alex.gaynor at gmail.com> wrote:
On Sat, Jul 14, 2012 at 4:18 PM, Benjamin Peterson <benjamin at python.org> wrote:
2012/7/14 Alex Gaynor <alex.gaynor at gmail.com>: > > Proposal > ======== > > This PEP proposes formally documenting
_lengthhint_
for other > interpreter and non-standard library Python to implement. > >_lengthhint_
must return an integer, and is not required to be > accurate. > It may return a value that is either larger or smaller than the actual > size of > the container. It may raise aTypeError
if a specific instance > cannot have > its length estimated. It may not return a negative value. And what happens if you return a negative value? ValueError, the same as with len.> > Rationale > ========= > > Being able to pre-allocate lists based on the expected size, as > estimated by >
_lengthhint_
, can be a significant optimization. CPython has been > observed to run some code faster than PyPy, purely because of this > optimization > being present. > > Open questions > ============== > > There are two open questions for this PEP: > > * Shouldlist
expose a kwarg in it's constructor for supplying a > length > hint. > * Should a function be added either tobuiltins
or some other module > which > calls_lengthhint_
, likebuiltins.len
calls_len_
. Let's try to keep this as limited as possible for a public API. Sounds reasonable to me! Should we just go ahead and strip those out now?
I'm +1 on not having a public API for this. Ultimately the contract for a length hint will depend heavily upon what you need it for. Some applications would require a length hint to be an "at least" others an "at most" and others something else entirely. Given that the contract here appears to be >=0, I don't think the length hint is particularly useful to the public at large.
-- Regards, Benjamin Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire) "The people's good is the highest law." -- Cicero
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/alexandre.zani%40gmail.com
- Previous message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Next message: [Python-Dev] PEP 0424: A method for exposing a length hint
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]