简体   繁体   中英

How can I use the result from a function in another function?

Suppose I have a function like:

def eklid(p, a, b,):
    x = [1, 0]
    y = [0, 1]
    r = [a, b]
    q = [0]
    n = 0
    while r[n+1] != 0:
        q.append(r[n] // r[n+1])
        r.append(r[n] % r[n+1])
        x.append(x[n] - x[n+1] * q[n+1])
        y.append(y[n] - y[n+1] * q[n+1])

        if p == 0:
            print(r[n], "=", r[n+1], "*", q[n+1], "+", r[n+2])
        elif p == 1:    # extended print
            print(r[n+2], "\t", x[n+2], "\t", y[n+2], "\t", r[n+2], "=", a, "*", x[n+2], "+", b, "*", y[n+2])
        elif p == -1:
            k =1
        else:
            print("wrong input")
        n += 1
    return x, y, r, q, n,

I want to use x and r from it in this function:

    def cong_solv(x, r, b,):
        result = x/r
        int_result = int(result)
        return int_result

How can I do that?

# Here, a=x, b=y, c=r, d=q, e=n
a, b, c, d, e = eklid(h, i, k)

# Assuming based on your function definitions you want the
# same value as the third argument
final_result = cong_solv(a, c, k)

You get the return values from eklid and save them into variables. You then use those variables to call the next function.

Of course, in a real code you should name your varialbes better than I did in this example. I deliberately did not call the variables the same names as inside the function to demonstrate that you don't have to.

One way would be to call the eklid() function from inside the cong_solv() function. Something like this should work:

def cong_solv(x, r, b):
   p = "foo"
   b = "bar"
   x, y, r, q, n = eklid(p, a, b)

   result = x/r 
   int_result = int(result) 
   return int_result

In python, when you return more than one variable, it returns a tuple. You can retrieve the value by its index (returned_value[0], returned_value[1]) or unpack the tuple like Mike Driscoll said (a, b, c, d = eklid(h, i, k)).

Since I got two downvotes, I am going to give you better (I hope) explanation: Everytime you return more than one value, it returns a tuple .

def my_function():
   a = 10
   b = 20
   return a, b

print type(my_function()) # <type 'tuple'>

But if you return just one value:

def my_function():
    a = 10
    return a

print type(my_function()) # <type 'int'>

So if you want to use your value, you can:

Unpack tuple values like this

a, b = my_function()

This way you get your return values in the same order you return inside my_function.

Rewriting your code, you can simply do:

a, b, c = eklid(10, 20, 30) # it will return a tuple

And call your other function:

cong_solv(a, b, 20)

In my honest opinion I would return a dict . With dict you can be explicit because your values have key names.

Inside your eklid return function:

return d # d = {"what_x_means": x,
         #      "what_y_means": y,
         #      "what_r_means": r,
         #      "what_q_means": q, 
         #      "what_n_means": n}

And retrieve for its key:

d["what_x_means"]
d["what_r_means"]

Similar to How do you return multiple values in Python?

  1. Return as an tuple (x,y,r....) or an array and assign to tuple / array respectively.
  2. Or assign them to class variables and access them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM