(12-16-2023, 01:05 AM)Dogarithm Wrote: IIRC, "pass" is used to replace a piece of code that is usually syntactically required, but for some reason or another, there is no instruction to give in practice. So "pass" is just telling the interpreter to run the next line of code instead. Like playing a game and choosing to skip your move. Break might be what you want instead to stop a loop. But in general, this is a recursion problem. For loops are a recursion, but they're really nice in that they often have terminal conditions built in, unlike while loops. ie,
Code:
for i in range(10):
i += 1
print(i)
There is no "recursion" here, just an iteration... In programming, recursion in when some piece of code calls itself. For instance you can define the even-ness of a positive integer as being the opposite of the even-ness of its predecessor, in other words,
isEven(i)=not isEven(i-1), so if you know the value for a number (for instance, 0), you can write the function like this:
def isEven(i):
return True if i==0 else not isEven(i-1)
(of course three are much more efficient and non-recursive ways to write the function).
(12-16-2023, 01:05 AM)Dogarithm Wrote: This has a terminal case, the result of that code will print the integer "9".
The result of your loop is
10, and this is proof that it is very dangerous and/or misleading in such a
for loop to alter the loop counter (it turns out that in this particular form Python resets
i to the next item from the
range()).
(12-16-2023, 01:05 AM)Dogarithm Wrote: The range function in the for loop gives an exact number of times the recursion is to be called
Technically the
range() gives you a list of items(*) and you execute the loop once for each item. It just happens that
range() produces an output of known size, with nicely ordered numbers.
(12-16-2023, 01:05 AM)Dogarithm Wrote: I don't what the pdb.gimp_image_delete(x) method does, but if it modifies the list in any way per iteration, that might be something to keep an eye on.
Good remark, but it depends on what you iterate (i.e., the thing after the
in in the
for ... in ... instruction). In this case it is a list that was obtained once for all (
gimp.image_list()).
(*) In Python v2. In Python v3,
range() is a "generator" that creates the required item only when needed so you don't create a large list just to iterate it. In Python v2, you can use
xrange() for this.