[STORY-TIME] THE BDFL AND HIS PYTHON PETTING ZOO

Chris Angelico rosuav at gmail.com
Thu Feb 18 16:54:50 EST 2016


On Fri, Feb 19, 2016 at 8:14 AM, Christian Gollwitzer <auriocus at gmx.de> wrote:
> Am 16.02.16 um 03:02 schrieb Rick Johnson:
>>
>> On Friday, February 12, 2016 at 1:51:35 AM UTC-6, John Ladasky wrote:
>>>
>>> I like lazy evaluation.
>>
>>
>> Well, it is a "Pythonic feature" no doubt.
>
>
>
> ?? I'm confused. Does Python have lazy evaluation? I thought that Python
> does eager evaluation. At least this snippet seems to confirm:
>
> def arg():
>         print("Evaluating arg")
>         return None
>
> def func(x):
>         print("Evluating func")
>         print(x)
>
> func(arg())
>
> If I run it, the output is:
>
> Evaluating arg
> Evluating func
>
> and I think that with side effects, only eager evaluation is really
> predictable.

Python's form of lazy evaluation comes in the form of functions that
return iterables, rather than concrete lists:

>>> def square(x):
...     print("Squaring %d..." % x)
...     return x*x
...
>>> squares = map(square, range(10))
>>> next(squares)
Squaring 0...
0
>>> next(squares)
Squaring 1...
1
>>> next(squares)
Squaring 2...
4

ChrisA


More information about the Python-list mailing list