0

Let's say I have a Python script yieldtest.py using a generator function:

def yieldfunc():
    yield 2
    yield 4
    yield 8
    yield 16

def main():
    nums = yieldfunc()
    for n in nums:
        print(n)

if __name__ == "__main__":
    main()

It works fine. But let's say something went wrong, so I want to debug it. So I type:

$ python -m pdb yieldtest.py

Then when I get to the line nums = yieldfunc(), I type s and hit Enter, which I would expect to bring me inside yieldfunc() so I can see what's occurring under the hood. That's not what happens, though. Instead it brings me to the next line in def main():

> /...path/yieldtest.py(8)main()
-> nums = yieldfunc()
(Pdb) s
> /...path/yieldtest.py(9)main()
-> for n in nums:
(Pdb) 

This means that I have to essentially black-box test all of my functions which use yield.

Is there any solution to this using the PDB debugger that will allow me to step into a generator function?

Lou
  • 2,200
  • 2
  • 33
  • 66
  • 1
    `yieldfunc` *doesn't run* at the point where you're trying to step into it. It sounds like you have a misunderstanding of what `yield` and generators actually do; you should [go read about that](https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do). – user2357112 Dec 23 '20 at 10:18
  • @user2357112supportsMonica Ahhh, I see my mistake. The function only runs when I iterate through it - so when I get to `for n in nums`, type `s` and hit Enter, *then* it will show me what's happening in `yieldfunc`. Thank you! – Lou Dec 23 '20 at 10:21

0 Answers0