简体   繁体   English

如何在另一个函数中使用函数的结果?

[英]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:我想在这个函数中使用它的xr

    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.您从eklid获取返回值并将它们保存到变量中。 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.一种方法是从 cong_solv() 函数内部调用 eklid() 函数。 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.在python中,当您返回多个变量时,它会返回一个元组。 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)).您可以通过索引 (returned_value[0], returned_value[1]) 检索值,或者像 Mike Driscoll 所说的那样解包元组 (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 .由于我得到了两个反对票,我将给你更好的(我希望)解释:每次你返回多个值时,它都会返回一个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.这样,您可以按照在 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 .老实说,我会返回一个dict With dict you can be explicit because your values have key names.使用 dict 你可以是明确的,因为你的值有键名。

Inside your eklid return function:在您的 eklid 返回函数中:

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?类似于如何在 Python 中返回多个值?

  1. Return as an tuple (x,y,r....) or an array and assign to tuple / array respectively.作为元组 (x,y,r....) 或数组返回并分别分配给元组/数组。
  2. Or assign them to class variables and access them或者将它们分配给类变量并访问它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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