Rodrigo Calderon
Rodrigo Calderon

Reputation: 37

Get the return in a function in other function?

Is there any way to access to the return in a function inside other function ?

Probably the next code explain more what I want to do.

class perro:
    def coqueta(self, num1, num2):
        self.num1 = num1
        self.num2 = num2
        return self.num1 + self.num2

    def otro_perro(self, coqueta):
        print otro_perro




mascota = perro()
ejemplo = mascota.coqueta(5,5)
mascota.otro_perro()

print ejemplo 

How can i get the return of the first def (coqueta) to print in the second function (otro_perro)?

Upvotes: 0

Views: 75

Answers (3)

mhawke
mhawke

Reputation: 87064

Either pass the return value of method coqueta() into otro_perro(), or have otro_perro() call coqueta() directly. Your code suggests that you wish to do the first, so write it like this:

class Perro:
    def coqueta(self, num1, num2):
        result = big_maths_calculation(num1, num2)
        return result

    def otro_perro(self, coqueta):
        print coqueta

mascota = Perro()
ejemplo = mascota.coqueta(5, 5)
mascota.otro_perro(ejemplo)

Or, you could call coqueta() from otro_perro():

    def otro_perro(self, num1, num2):
        print self.coqueta(num1, num2)

but that requires that you also pass values for num1 and num2 into otro_perro() as well.

Perhaps num1 and num2 could be considered attributes of class Perro? In that case you could specify them when you create the class:

class Perro:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

    def coqueta(self):
        result = big_maths_calculation(self.num1, self.num2)
        return result

    def otro_perro(self, coqueta):
        print self.coqueta()    # N.B. method call

Or another possibility is to cache the result of the "big calculation":

class Perro:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2
        self.result = None

    def coqueta(self):
        if self.result is None:
            self.result = big_maths_calculation(self.num1, self.num2)
        return self.result

    def otro_perro(self, coqueta):
        print self.coqueta()    # N.B. method call

Now the expensive calculation is performed just once when required and its result is stored for later use without requiring recalculation.

Upvotes: 2

Chris
Chris

Reputation: 22953

Just make it an attribute of the perro class. Thats really the whole point of class, to modularize, organize, and encapsulate your data instead of having to use globals everywhere:

class perro:
    def coqueta(self, num1, num2):
        self.num1 = num1
        self.num2 = num2
        self._otro_perro = self.num1 + self.num2

    def otro_perro(self):
        print self._otro_perro




mascota = perro()
mascota.coqueta(5,5)
mascota.otro_perro() # will print the value of self._orto_perro

I added the extra underscore before orto_perro because you had already used that name for your method. And as a unrelated side note, class names a gereral capitalized in Python. So perro would become Perro.

Upvotes: 0

brainless coder
brainless coder

Reputation: 6430

Whats wrong with this?

mascota = perro()
ejemplo = mascota.coqueta(5,5)
mascota.otro_perro(ejemplo)

or,

mascota = perro()
mascota.otro_perro(mascota.coqueta(5,5))

Upvotes: 0

Related Questions