Who first executes the condition or code block in Python?

Asked

Viewed 119 times

-1

I’m having a problem and I believe it’s because of the execution order I can’t run such a block of code if it doesn’t go through a condition first if it won’t go wrong, and once I hear that in C it first runs the code then checks, is that true? And how it is in python?

 menor = 9999999999999999999999999999
    valor_k = 0
    for k in range(self.numero_vertices): 
        if (grafo.number_of_edges(v,k) == 1): # se for diferente de 1 não deveria fazer nada so aumentar o valor de k no for
            if (grafo[v][k]['tamanho'] < menor and self.verifica_fonte(grafo, k, v) == True):
                menor = self.grafo[v][k]['tamanho']
                valor_k = k
    return valor_k
  • It depends on what you’re talking about, show the code and what you think is first.

  • I edited the question and put the code

  • What don’t you understand? What do you think you do first?

  • 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.

  • Explain further what you have heard. None of this seems to be true, including why it would be inconsistent.

  • 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.

  • 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

Show 2 more comments

1 answer

0


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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.