简体   繁体   中英

pydev can't step in generators

I'm using pydev on Windows 7 x64,and I found that breakpoints within generator functions are ignored(if I comment out yield ,everything worked fine).

Then I found an old SO question Does Python debugger step in generators?

The answer says "I just tested eclipse and it will do debugging with pydev installed."

But when I tested the code,breakpoints are still ignored.

def example(n):
    i = 1
    while i <= n:
        yield i
        i += 1

print "hello"

print "goodbye"

if __name__ == '__main__':
    example(8)

So my question is:

  1. Is PyDev able to step in generators?
  2. If not, what should I do to debug such code?

When I run exactly the code in the "With the Generator" section of the old question (not what you have in your question) with the debugger and put a breakpoint on the i += 1 statement, it does stop running there and will continue to do so each time I press F8 to resume until the generator is exhausted. I'm using PyDev for Eclipse 2.8.2.2013090511.

The problem is that your code only calls the generator function once, which only returns an iterator object -- it doesn't actually execute the code in the function. To do that you need to iterate the returned object somehow -- either implicitly via a for statement or explicitly by calling its next method. See the paragraph that starts with "When you call a generator function, it doesn't return a single value;…" in the Generators section of the documentation.

Here's the code I used:

def example(n):
    i = 1
    while i <= n:
        yield i
        i += 1

print("hello")

for n in example(3):
    print(n)

print("goodbye")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM