Why do breakpoints in my function with a yield do not break?
Question:
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…
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?
Answers:
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()))
func1
is a generator function. It returns a generator; it does not automatically execute the for
loop. The first time you call next
on the generator, it will execute up to the first yield
statement, which produces the return value for next
. Each subsequent time you call next
on the same generator, it resumes where it left off and continues executing until the next yield
statement (in this case, in the next iteration of the loop).
So you would need to write something like
g = func1()
next(g)
to hit your breakpoint for the first time.
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…
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?
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()))
func1
is a generator function. It returns a generator; it does not automatically execute the for
loop. The first time you call next
on the generator, it will execute up to the first yield
statement, which produces the return value for next
. Each subsequent time you call next
on the same generator, it resumes where it left off and continues executing until the next yield
statement (in this case, in the next iteration of the loop).
So you would need to write something like
g = func1()
next(g)
to hit your breakpoint for the first time.