How to exit a function and go to another function within a class, python?

Asked

Viewed 170 times

-2

would like a help I’m new in python programming, I used to program in excel(VBA) and have an instruction in VBA called "Goto" that helped me a lot, for example arrived at a certain part of the code that depending on the result or if of this error I wrote Goto PARTE_1 and it went exactly to the part of my code where it was written PARTE_1: and did not return, just went ahead. What I need is to know how to do this in python.

  • This call as I understand it is a Goto, what you don’t have in Python, what you can try is the @de_python answer. Remembering that Goto is not considered a good practice

  • Good guy, misspelled, it’s exact Goto, thank you

  • The bigger question is in what moment you miss a drop when working with Python. It’s within a loop? break and continue are better ways of flow control. Within a function? Use return to get out of it at any point. parole? Put the code to which the goto would redirect into the condition (better still, in a function to be called conditionally). Avoid errors? Is there a try and except for that reason.

  • better Paste some of the code you want to work - small anoints with just one if and print when you need to, to create a complete and minimal example. Otherwise a correct answer turns into a full functional programming class, and not necessarily you will understand, why is it unclear where you are missing.

2 answers

0

I managed, thank you all, so the code, excludes the class, I created a while that runs while the counter is smaller than the number of lines and inside it I put if and Elif calling the functions according to the variable "a" and the functions stayed inside Try and inside the functions themselves that have the code I put the Return to exit the function in moments of error or delay to load or etc... and when it returns it respects the Try that tells exactly what to do modifying the variable "a"

I do not know if I explained well or if the code was ideal but it is the first code in . py that I do, worked out but with time I improve. Thanks again, you were instrumental.

a = 5
while CONTADOR_CNPJ <= dfcount:

    if a == 0:
        try:
            consulta(CONTADOR_CNPJ)
            a = 1
        except:
            a = 15
    elif a == 1:
        try:
            CLICA_CLIENTE()
            a = 2
        except:
            a = 15
    elif a == 15:
        try:
            ERRO()
            a = 0        
        except:
            a = 5            
    elif a == 2:
        try:
            GRAVAR = COPIA_DADOS(y)
            a = 3
        except:
            a = 15

    elif a == 3:
        try:
            CONTADOR_CNPJ = FIM(GRAVAR, CONTADOR_CNPJ)
            a = 0
        except:
            a = 15

    elif a == 10:
        try:
            CNPJ = df.loc[CONTADOR_CNPJ, 'CNPJ']
            CNPJ = str('%014d' % int(CNPJ))
            arq.write(CNPJ + "||" 'NAO ENCONTRADO')
            CONTADOR_CNPJ = CONTADOR_CNPJ + 1
            a = 0
        except:
            a = 15

    elif a == 4:
        try:
            iden()
            a = 0
        except:
            a = 5

    elif a == 5:
        try:
            nav = webdriver.Chrome()
            logar(login, senha)
            a = 4
        except:
            a = 15

0

You can create a function with this specification, it can be up to a function called parte_1, example:

class MinhaClasse:

    def __init__(self, atributo1, atributo2):
        self.atributo = atributo1
        self.atributo2 = atributo2

    def funcao_primaria(self):
        x = None
        fazendo alguma coisa
        fazendo outra coisa que faz x = True ou x = False

        if x == True:
            self.parte_1()
        else: #se x == False
            self.parte_2()

    def parte_1(self):
        fazendo alguma coisa própria da função
        ...
        return resultado

    def parte_2(self):
        fazendo alguma coisa própria que não é esperado que parte_1 faça
        ...
        return resultado

If you don’t have a class, just remove the function init and all code self as below:

def funcao_primaria():

    x = None
    fazendo alguma coisa
    fazendo outra coisa que faz x = True ou x = False

    if x == True:
    parte_1()
    else: #se x == False
    parte_2()

def parte_1():

    fazendo alguma coisa própria da função
    ...
    return resultado

def parte_2():

    fazendo alguma coisa própria que não é esperado que parte_1 faça
    ...
    return resultado

Browser other questions tagged

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