Get the return in a function in other function?
<p>Is there any way to access to the return in a function inside other function ?</p>

<p>Probably the next code explain more what I want to do.</p>

<pre><code>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 
</code></pre>

<p>How can i get the return of the first <code>def</code> (<code>coqueta</code>) to print in the second function (<code>otro_perro</code>)?</p>

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

<pre><code>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)
</code></pre>

<p>Or, you could call <code>coqueta()</code> from <code>otro_perro()</code>:</p>

<pre><code>    def otro_perro(self, num1, num2):
        print self.coqueta(num1, num2)
</code></pre>

<p>but that requires that you also pass values for <code>num1</code> and <code>num2</code> into <code>otro_perro()</code> as well.</p>

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

<pre><code>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
</code></pre>

<p>Or another possibility is to cache the result of the "big calculation":</p>

<pre><code>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
</code></pre>

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

python function