[IronPython] Length Hint Not Used

Curt Hagenlocher curt at hagenlocher.org
Fri Oct 31 23:18:20 CET 2008


The CPython test suite says specifically

# note: this is an internal undocumented API
# don't rely on it in your own programs

so I don't feel too bad about it. :)

On Fri, Oct 31, 2008 at 3:12 PM, Michael Foord <fuzzyman at voidspace.org.uk>wrote:

> Hello,
>
> Well, I guess this isn't a bug (its only a hint) - but IronPython doesn't
> use length hint. :-)
>
> CPython
> >>> class X(object):
> ...  def __iter__(self):
> ...   return iter([1,2,3])
> ...  def __length_hint__(self):
> ...   print 'length hint'
> ...   return 3
> ...
> >>> x = X()
> >>> list(x)
> length hint
> [1, 2, 3]
>
> IronPython
> >>> class X(object):
> ...  def __iter__(self):
> ...   return iter([1,2,3])
> ...  def __length_hint__(self):
> ...   print 'length hint'
> ...   return 3
> ...
> >>> x = X()
> >>> list(x)
> [1, 2, 3]
> >>>
>
> CPython uses it as an optimisation to preallocate space for the list.
>
> You can bet somebody, somewhere is writing code that depends on length hint
> being called... Actually probably not, but it's one of those methods like
> '__reversed__' and '__missing__' that I've only recently discovered. I was
> impressed that IronPython supported '__missing__' by the way.
>
> Michael
>
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081031/cfcc0c68/attachment.html>


More information about the Ironpython-users mailing list