Lazy evaluation of map

前端 未结 3 678
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 20:02

I recently read that one benefit of map in Python 3 was that it is lazy. That means, it is better to do

map(lambda x: x**2, range(10**100))
         


        
3条回答
  •  囚心锁ツ
    2020-12-06 20:13

    First, please note that range (xrange in Python 2) is a special case. It's not a simple generator, nor does it just return a list. It supports in operations as well, which is not a standard feature of iterables or iterators.

    Consider that map(func, iterable) could be called on an infinite iterable, or an iterable where the process of fetching the next value is a time consuming process.

    You'd need to be aware that your function might deal with these types of values, and make sure to use a lazy function, like itertools.imap otherwise. Since its basically impossible to determine that an iterator is infinite, even at runtime, shouldn't the builtin function behave correctly for the widest range of inputs?

    Not every use-case requires random access, and those that do must fully instantiate the iterable or use another itertools function like islice.

提交回复
热议问题