0

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.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Smithy
  • 1,157
  • 3
  • 11
  • 18
  • 2
    `distinct(items)` creates a generator. When you iterate the generator, the code inside `def distinct(...)` is executed. That is the effect of having a `yield` statement in your function. – khelwood Jul 21 '22 at 13:26
  • I don't understand what "the call to distinct was not being evaluated first but rather called from distinct" means. – Scott Hunter Jul 21 '22 at 13:28
  • I put print("take") and print("distinct") statements at the top of the relevant functions and take was printed first followed by distinct whereas I expected the opposite. – Smithy Jul 21 '22 at 13:43

0 Answers0