mmswe
mmswe

Reputation: 707

Multi-entry, multi-exit co-routine in Python

def print2Combs(n):
    for i in range(0, n):
        for j in range(i+1, n):
            print (i,j)

print2Combs(5)

That code gives me this output:

0 1
0 2
0 3
0 4
1 2
1 3
1 4
2 3
2 4
3 4

Supposedly, this function is a co routine and it is a multi entry, multi exit function. And it is a generator. I failed to see the connections and can't answer followings.

  1. Why it is a multi entry function?
  2. Why it is a multi exit function?
  3. Why it is a co routine?

Any help is appreciated. Thank you!

Upvotes: 0

Views: 189

Answers (1)

unutbu
unutbu

Reputation: 879691

A generator is a function that returns an iterator.

print2Combs returns None. None is not an iterator, so print2Combs is not a generator.

A coroutine is a kind of generator which allows values or exceptions to be passed in when execution resumes.

Since print2Combs is not a generator, it can not be a coroutine.

Upvotes: 1

Related Questions