I came across some Python code which I don't quite understand and would appreciate help with.
def take(count, iterable):
counter = 0
for item in iterable:
if counter == count:
return
counter += 1
yield item
def distinct(iterable):
seen = set()
for item in iterable:
if item in seen:
continue
yield item
seen.add(item)
def run_pipeline():
items = [3, 6, 6, 2, 1, 1]
for item in take(3, distinct(items)):
print(item)
run_pipeline()
When I was debugging the code I noticed that when calling take
, the call to distinct
was not being evaluated first but rather called from distinct
. I could understand such behaviour had I passed distinct
as just a name which would have referenced the object. I come from a C background where distinct(items)
would be called before take
.