Access the iterator object in the debugger

Is there no way to access the iterator object or its state from within the for loop?

Using pdbor ipdb, for example, I can niterate over and over again with help , but not see which iteration I am in (ex post, I mean, of course, I could use an enumeration, but only before starting the debugger).

defcreates an object, and fordoes the same, doesn't it? But the function has a name - and the iterator does not, is not available in memory? By the way, is a function accessible from inside her body without knowing the name?

(Answers to Python questions : accessing the iterator object in for-loops and Iteration again in the for loop suggest that this is not possible, but it seems to be very strange, I'm used to having to check something in python.)

-1
source share
1 answer

What do you think the "internal state" of the iterator looks like?

  • If I repeat the lines read from the file, it will be some kind of buffer file and the position of the character in the current buffer.

  • If I iterate over the Fibonacci sequence, these will be the last two values ​​used to calculate the next.

  • If I iterate over a database query, the result may be the database cursor used to read the next row.

  • , .

  • , .

Python. , , . , , next() , ( , ).

next() for, , for v in someiterable: ... :

iterator = iter(someiterable)
for v in iterator:
    ... # Can refer to `v` or even call `v.next()` here ...

, Python , . , for , , . (, - , for, , .)

+1

Source: https://habr.com/ru/post/1791804/


All Articles