Doubt in the functioning of a Python code (Classes and objects)

Asked

Viewed 66 times

3

Guys, I don’t understand something in this code. I’m importing the Client and Account classes into another program to use them. But I could not understand why it is working, and the deposit method is 'called'' once before it has even been defined.

class Cliente:

    def __init__(self, nome, telefone):
        self.nome = nome
        self.telefone = telefone


class Conta:

    def __init__(self, clientes, numero, saldo = 0):
        self.saldo = saldo
        self.clientes = clientes
        self.numero = numero
        self.operacoes = []
        self.deposito(saldo)  # AQUI ELE SENDO UTILIZADO

    def resumo(self):
        print('CC Numero: %s Saldo: %10.2f'
              %(self.numero,self.saldo))

    def saque(self, valor):
        if self.saldo >= valor:
            self.saldo -= valor
        self.operacoes.append(['Saque', valor])

    def deposito(self, valor):  # SÓ AQUI ELE É DEFINIDO
        self.saldo += valor
        self.operacoes.append(['Deposito',valor])

    def extrato(self):
        for x in self.operacoes:
            print(x[0], x[1])

1 answer

3


It seems to me you’re making a mess between function statement and function call.

In your example the functions are being only declared. They’re not being called to execution. Thus, there is no problem with the deposit function appearing within another function a little higher, because it is not called, that is, it will not be executed.

Interpret this as an index of functions that the program has to do what it was designed to do. Simple as this.

Now, when the function __init__ is executed, yes, in this case all necessary data such as balance, customers, operation, etc. should have already been defined. By logic, the banking system will query the necessary data at the time of the user’s login, that is, any function that is called from there will already be fully supplied with necessary data.

Browser other questions tagged

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