2019-05-19 04:55:44

So, up till now I have been  making if statements that would change a boolean if the boolean has not been already set to that state.  For example:

something=False
for i in range(10):
 if i>=5 and not something: something=True
 This would trigger only when something is False, consider this bit of code however: 
something=False
for i in range(10):
 if i>=5: something=True

would the loop effectively set something equal to true every time I is greater than five even though something may equal true already?  Or does a check internally if the variable is already set to that value?

2019-05-19 05:17:31

it would set each time through.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2019-05-19 06:23:55 (edited by magurp244 2019-05-19 06:27:02)

Both of those examples would do the same thing. In the first case if I isn't greater than or equal to 5, the if statement won't trigger because both statements need to be true because you put "and" between them. So if something is False it will trigger when I is greater than or equal to 5, and if something is True, it won't matter because the purpose of the if statement is to make something true anyway, which makes checking if something is true or false pointless. Now, if you put an OR statement between the checks, then whenever something is False it would set it to True, making the I is greater than or equal to 5 statement irrelevant.

The second statement skips all this by just checking if I is greater than or equal to 5, which sets something to True whether it is already or not, which ultimately doesn't matter.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-05-20 23:05:33

Technically it sets it, but I'd imagine that checking each time whether it's true vs resetting it to true even if it is already true would execute at roughly the same speed, so you can do either, at least for things like booleans and other simple variables. But once you get into more advanced objects that must be initialized, you'll want the first approach as it will run considerably faster. For example:
bio=None
for i in range(10):
    if i>5 and not bio: bio=io.BytesIO(someLongString)
would execute much faster than if you had removed that second claws of the statement, because making a new BytesIO object with your string is internally harder work than assigning to a bool. I also tested this, it seems the first approach when comparing rather than always setting, even with bool, run 10 times is just a couple ms faster than the second approach, where you set it no matter what. The difference is so small in that case it probably doesn't matter though, however I'd still avoid it in longer loops, or programs where your goal is efficiency rather than simple code that just works. But you should really avoid it when actually making new objects, as that is much more intensive. and should only be done if your object doesn't already exist.

I am a web designer, and a game developer. If you wish see me at http://www.samtupy.com