PDA

View Full Version : [pyQt] Trying to understand the a piece of code



optagon
20th December 2013, 09:34
I am editing an texture exporter that somebody else coded, and there's a small segment I'm not grasping.
The exporter has a QListView that get's filled with groups from photoshop.

I had removed a part of the code that I thought was purely for debuging, but then I noticed that I started to get duplicate entries in the list. Here:


if layerName in loadedNames:
print layerName, " IS ALREADY LOADED"
continue

if layerName.endswith("_alpha") or layerName.endswith("_a"):
print "SUPPOSED TO MOVE INTO ALPHA"
continue

All I see is it printing messages to the console. The if's don't do anything else... Perhaps the issue here is that I don't understand how continue works. I thought it was just something people wrote for clarity, and to avoid nesting if statements. Any ideas?

anda_skoa
20th December 2013, 10:08
The ifs do something else, they continue the loop they are in, i.e. they advance the loop and make it execute the loop body from the beginning again. Basically skipping the current loop iteration.

If you take them out then the control flow will reach statements after those ifs, in your case most likely code that adds data to the list view.

Cheers,
_

optagon
20th December 2013, 10:21
I see, thank you. So it's basically the same as writing "if not... " followed by a break.

anda_skoa
21st December 2013, 10:05
A break ends the loop, so no.

The equivalent would be an "if not" with the rest of the code in its "then" body

Cheers,
_

DrTebi
24th December 2013, 05:56
As mentioned by anda_skoa, break and continue are not the same.

Both are used in loops, but do different things:

break: Stops the iteration of the loop, and continutes to whatever code statement below the loop.
continue: Continues the iteration of the loop, but will ignore any code statements below the 'continue' keyword.

So basically what this means for this piece of code is:
1. if statement: checks if the value of the 'layerName' variable already exists in the list 'loadedNames', and if so, it skips all further statements and continues the loop with the next 'layerName', otherwise it goes to the next if statement.
2. if statement: checks if the value of the 'layerName' variable ends with "_alpha" or ends with"_a", and if so, it skips all further statements and continues the loop with the next 'layerName', otherwise it goes to whatever comes after this if statement.

It's really quite easy to understand, turn your programmer mind off and read the code... 'endswith' 'continue' etc. will then probably make more sense :)


-