On 01Mar2021 00:28, Peter Otten <__pete...@web.de> wrote: >On 28/02/2021 23:33, Marco Sulla wrote: >>I can't reproduce, Am I missing something? >> >>marco@buzz:~$ python3 >>Python 3.6.9 (default, Jan 26 2021, 15:33:00) >>[GCC 8.4.0] on linux >>Type "help", "copyright", "credits" or "license" for more information. >>>>>class A: >>... def __len__(self): >>... return 1024**3 >>... def __iter__(self): >>... yield from () >>... >>>>>a = A() >>>>>len(a) >>1073741824 >>>>>list(a) >>[] >>>>> >> >>It takes milliseconds to run list(a) > >Looks like you need at least Python 3.8 to see this. Quoting >https://docs.python.org/3/whatsnew/3.8.html: >""" >The list constructor does not overallocate the internal item buffer if >the input iterable has a known length (the input implements __len__). >This makes the created list 12% smaller on average. (Contributed by >Raymond Hettinger and Pablo Galindo in bpo-33234.) >"""
That may also explain why I hadn't noticed this before, eg last year. I do kind of wish __length_hint__ overrode __len__ rather than the other way around, if it's doing what I think it's doing. Cheers, Cameron Simpson <c...@cskk.id.au> -- https://mail.python.org/mailman/listinfo/python-list