How to run a def with multiple values only once and use values into another def?
<p>My problem is real simple but unfortunately i can't find a way to solve it.</p>

<p>I would like to run a def A which returns multiple values from def B only once.</p>

<p>I wrote this block of code :</p>

<pre><code>def A():
    x = 1
    y = 2
    z = 3
    return a,b,c

def B():
    d = A[0] + A[1]
    e = A[2] - A[0]
    print d,e

B()
</code></pre>

<p>If i'm using this block of code it will run four times!</p>

<p>Thanks in advance.</p>

<p>Simple, there are mistakes in your code, <code>A</code> is a function and you should call it with <code>A()</code> to get the returned values. Instead you use it like <code>A[0]</code> which is un-subscriptable, and you can assign a temp variable within <code>B</code> function so you can re-use the returned values, change to below should work:</p>

<pre><code>In [54]: def A():
    ...:     x = 1
    ...:     y = 2
    ...:     z = 3
    ...:     return x,y,z  # I fixed your typo too
    ...:
    ...: def B():
    ...:     a = A()
    ...:     d = a[0] + a[1]
    ...:     e = a[2] - a[0]
    ...:     print(d,e)
    ...:
    ...: B()

    3 2
</code></pre>

python return