Is there a way to pass a class instance as a parameter to a method in Python?

1

I'm doing an implementation of the algorithm A Star (A *).

My question is just the title, I come from java and c ++, and I do not know how to do this in python.

def transfTo(Bucket a): <------------Dúvida aqui!
        transf = a.total - a.current
        if current >= transf:
            a.current = a.total
            current -= transf
        else:
            a.total += current
            current = 0
    
asked by anonymous 17.04.2018 / 19:57

1 answer

1

Yes, the difference is that in python typing is dynamic, this comes from Duck typing

Here is an example passing a class as a parameter

class Pessoa:
    def __init__(self):
        pass

    def setNome(self, nome):
        self.nome = nome

    def setIdade(self, idade):
        self.idade = idade

    def getNome(self):
        return self.nome

    def getIdade(self):
        return self.idade

def meu_metodo(pessoa):
    print(pessoa.getNome())
    print(pessoa.getIdade())

def meu_outro_metodo():
    pessoa = Pessoa()
    pessoa.setNome("Leonardo")
    pessoa.setIdade(23)
    meu_metodo(pessoa)


meu_outro_metodo()

In the example above, there is a class, In the 1st method the class is "instantiated" and has its values set, after that another method is called, passing the class in the parameter, and in that other method, the values < p>     

17.04.2018 / 21:07