[Python-Dev] PEP 0424: A method for exposing a length hint (original) (raw)
Mark Shannon mark at hotpy.org
Sun Jul 15 16:14:59 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 ]
Alex Gaynor wrote:
Hi all,
I've just submitted a PEP proposing making lengthhint a public API for users to define and other VMs to implement:
These seems back-to-front. length_hint is used by the VM, not provided by it. It should be part of the object model, rather than the API.
PEP: 424 Title: A method for exposing a length hint Version: RevisionRevisionRevision Last-Modified: $Date Author: Alex Gaynor <alex.gaynor at gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 14-July-2012 Python-Version: 3.4 Abstract ======== CPython currently defines an
_lengthhint_
method on several types, such as various iterators. This method is then used by various other functions (such asmap
) to presize lists based on the estimated returned by
Don't use "map" as an example. map returns an iterator so it doesn't need length_hint
_lengthhint_
. Types can then define_lengthhint_
which are not sized, and thus should not define_len_
, but can estimate or compute a size (such as many iterators).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.
Rather than raising a TypeError, why not return NotImplemented?
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_
. Copyright ========= This document has been placed into the public domain. .. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8Alex
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/mark%40hotpy.org
- 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 ]