it first executes the code then checks
That’s not true, I believe for no language.
if 1 > 2:
print('Isso nunca vai ser executado.')
In the example above, the print
will not be executed.
If the code inside your if
is being executed when it should not, it is because the condition evaluated by him has resulted in True
. I suggest you use a Bugger or print
in its variables to understand what is happening and where is the wrong assumption.
As for what you heard, you probably misunderstood the following:
What may happen is that maybe you check a condition so that it has side effects. For example:
def verificar_xyz():
print('Isso sempre será executado.')
return False
if verificar_xyz():
print('Isso nunca será executado.')
In this case, the block inside the if
will not be executed because verificar_xyz
returns False
, but the print
from within the function is executed, because the function is normally executed until the return
.
In a if
or another condition check, if one of the conditions in a and
is negative, the evaluation stops immediately. This means that, for example, the following sections behave differently:
if verificar_xyz() and 1 > 2:
print('Isso nunca será executado.')
Here the block inside the if
will not be executed, but the function verificar_xyz
was executed and the text "This will always be executed" from inside the function is shown on the screen. 1 > 2
is not enough to be evaluated because the first condition evaluated has already returned False
.
if 1 > 2 and verificar_xyz():
print('Isso nunca será executado.')
Here the "This will always be executed" function verificar_xyz
nay will be executed because since 1 > 2
results in False
, Python (or other language) knows the condition and
cannot be true and immediately skips the block without evaluating verificar_xyz()
.
Therefore, it is recommended that functions used in condition evaluations do not have side effects. This can generate subtle bugs.
It depends on what you’re talking about, show the code and what you think is first.
– Maniero
I edited the question and put the code
– Diego Dias
What don’t you understand? What do you think you do first?
– Maniero
When it arrives in my first condition I’m thinking that it does not check it but does the rest of the code then back in it to check if really my function returned 1 or not. I heard once that in the C language he did it because it was more "economical". I wonder if this is true and if in python it is also like this.
– Diego Dias
Explain further what you have heard. None of this seems to be true, including why it would be inconsistent.
– Maniero
I think it really must be something in my code that’s wrong, I was also thinking it wasn’t that, but I asked to be sure, anyway I appreciate your help. I’m gonna sit here and watch where I’m going wrong, thank you.
– Diego Dias
After a lot of trying I discovered that the error, was that I was forgetting to assign a value to a variable in the constructor of my class. huhuehueheu, I was beginning to think it was another world’s mistake
– Diego Dias