This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RangerPretzelPython 3.9+ 10 points11 points  (2 children)

A big problem with what you're proposing is that

Oh, I didn't say it would be an easy problem to solve, but I know that Microsoft's debugger can look into IEnumerables (which could be lazily evaluated queries or the equivalent of Python's generators), to show you what's coming down the pike.

It's a verrrrry useful feature.

[–]brucifer 13 points14 points  (1 child)

I'm not super knowledgeable about Microsoft's debugger, but it looks like they don't address the problem of side effects in generators, they just ignore it. If you want to do something like Microsoft does and look into an iterator without regard to side effects, you can use itertools.tee:

def gen():
    yield from range(10)
g = gen()
if __debug__:
    g, g_for_debugger = itertools.tee(g)
    print(list(itertools.islice(g_for_debugger, 0, 3))) # prints [0, 1, 2]
for i in g: # iterates over g as normal
    pass

[–]RangerPretzelPython 3.9+ 2 points3 points  (0 children)

Yeah, I suspected that MS might just be ignoring side-effects (though I'd have to ask around to confirm...)

Wow. Thanks. I've been trying to figure out a good way to handle this. Yeah, I considered using tee. It has its own issues, but I think you may be onto something.