简体   繁体   English

从函数中的函数调用变量

[英]Calling a variable from a function in a function

Quick question.快速提问。 I first defined a function prc_chgd that ultimately gives me a variable ans , but as an intermediate step gives me the variable price .我首先定义了一个函数prc_chgd ,它最终给了我一个变量ans ,但作为中间步骤给了我变量price Now, I want to write a subsequent function hdg , that using the same order of inputs, used the intermediate variable price to calculate something else.现在,我想编写一个后续函数hdg ,它使用相同的输入顺序,使用中间变量price来计算其他东西。 Unfortunately, I don't know how :S不幸的是,我不知道如何:S

def prc_chgd(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    price = xyz
    ans   = price*abc

def hedge(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    from prc_chgd(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y) import price
    ans = price*xxxx

the point is that the functions are two separate excercises within a notebook assignment, and I might have to recalculate the preceding function with new inputs (in the same order though), and only choose the defined variable price for my calculation.关键是这些函数是笔记本分配中的两个单独的练习,我可能不得不用新的输入重新计算前面的函数(尽管以相同的顺序),并且只为我的计算选择定义的变量价格

Thanks a bunch in advance!提前致谢!

I cannot completely follow, but I think your xyz is some mysterial combination of p0, ta, ya etc.我不能完全理解,但我认为你的xyzp0, ta, ya等的神秘组合。

In this case, you should put the price calculation into a separate function such as在这种情况下,您应该将价格计算放在一个单独的函数中,例如

def calc_price(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    return xyz # calculated with these inputs

def prc_chgd(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    price = calc_price(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y)
    ans = price * abc

def hedge(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    price = calc_price(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y)
    ans = price * xxxx

If you want to avoid to have your price be computed twice, you could also do如果你想避免你的price被计算两次,你也可以这样做

def calc_price(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    return xyz # calculated with these inputs

def prc_chgd(price):
    ans = price * abc

def hedge(price):
    ans = price * xxxx

and leave it to the caller how often calc_price() is called.并将它留给调用者调用calc_price()频率。

You can return more than one value from your function if you want like this:如果你想这样,你可以从你的函数中返回多个值:

def prc_chgd(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    price = xyz
    ans   = price*abc
    return price, ans


def hedge(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
    price, ans = prc_chgd(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y)
    ans = price*xxxx

Function that returns multiple return values should yield in python:返回多个返回值的函数应该在 python 中产生:

def prc_chgd(p0, ta, ya, tb, yb, cb, delta1_y, delta2_y):
   price = xyz
   yield price
   ans   = price*abc
   yield ans

You can yield as many values as you like.您可以根据需要生成任意数量的值。 Then you can iterate over all the answers in a for loop:然后您可以在 for 循环中迭代所有答案:

for total in prc_chgd(1,2,3,4,5,6,7,8):
   print total

First it will print the price, then price*abs.首先它会打印价格,然后是 price*abs。 Since the 2 functions belong together, I recommend to use a class.由于这两个函数属于一起,我建议使用一个类。

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

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