Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do breakpoints in my function with a yield do not break?

I am writing Python in VSCode. I have a file with two functions, and a second file that calls those function. When I put breakpoints in one of the functions, the breakpoint does not hit.

Here is test2.py that has the two functions…

def func1():
    for i in range(10):
        yield i

def func2():
    for i in range(10):
        print(i)

Here is test1.py for which the debugger is being launched from…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import test2

test2.func1()
test2.func2()

When I put a breakpoint on the for loop in both func1 and func2, then run the debugger from test1.py, the breakpoint for func1 is never hit, but the breakpoint in func2 is hit.

Why is this?

>Solution :

You never actually advanced the generator, so it set up a generator instance for func1, but didn’t begin executing it. If you want it to run out the generator, iterate the result of calling func1, e.g. replace:

test2.func1()

with:

# Extracts and prints all items
for x in test2.func1():
    print(x)

# Or to extract and print a single item:
print(next(test2.func1()))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading