Unexpected return of the method

Asked

Viewed 28 times

0

I am learning POO in python, I have created a client class that has three attributes; name, Cpf and age. And I also created a method that stores these attributes inside a tuple and returns this tuple. But the method did not return the tuple, it returned this:

<bound method Cliente.dados of <__main__.Cliente object at 0x7f8788c67940>>

I can’t understand why.

Method:

def dados():
        """
        método criado para retornar todos os dados em uma tupla
        """
        dados = (self.nome,self.idade,self.cpf)
        return dados

Instance

clientes = Cliente('Joaquin',21,'04040505')

print(clientes.dados)

1 answer

1


The mistake runs because you’re trying to print in function instead of you calling her

the correct would be

clientes = Cliente('Joaquin',21,'04040505')

print(clientes.dados())

something else its function is wrong, I believe that the code is correct, but lacked a Paramento, which by convention must be passed whenever a method/function is created the `self.

def dados(self):
        """
        método criado para retornar todos os dados em uma tupla
        """
        dados = (self.nome,self.idade,self.cpf)
        return dados

can be better informed in stackoverflow

  • It worked now, but because I have to put the self as a parameter?

  • 1

    @eudsonduraes https://answall.com/q/176543/112052

  • 1

    Dude, it’s but a convention, it’s not mandatory. correct on the answer, but it’s to say that it belongs to a class, in that link explains well

  • 1

    In fact, if it is a method there should always be a first parameter that will be reference to the instance itself; only the name self convention.

Browser other questions tagged

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