[Python-ideas] Suggested MapView object (Re: __len__() for map())

Chris Angelico rosuav at gmail.com
Sat Dec 1 20:24:19 EST 2018

On Sun, Dec 2, 2018 at 12:08 PM Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> class MapView:
>      def __len__(self):
>          return min(map(len, self.args))
>      def __iter__(self):
>          return self
>      def __next__(self):
>          if not self.iterator:
>              self.iterator = map(self.func, *self.args)
>          return next(self.iterator)

I can't help thinking that it will be extremely surprising to have the
length remain the same while the items get consumed. After you take a
couple of elements off, the length of the map is exactly the same, yet
the length of a list constructed from that map won't be. Are there any
other non-pathological examples where len(x) != len(list(x))?


More information about the Python-ideas mailing list