1

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. 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. Unfortunately, I don't know how :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!

1
  • 1
    Where is hedge called from? You'll need to return price from prc_chgd if you want it to be used elsewhere. Commented Mar 19, 2020 at 13:28

3 Answers 3

2

I cannot completely follow, but I think your xyz is some mysterial combination of p0, ta, ya etc.

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

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

looks good in theory, but in my assignment the first function is evaluated on the output of prc_chgd, and will only take one value. thus, if I return two values in the function body, I get an error message from my assignment saying it expects only one value as output.
-1

Function that returns multiple return values should yield in 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 total in prc_chgd(1,2,3,4,5,6,7,8):
   print total

First it will print the price, then price*abs. Since the 2 functions belong together, I recommend to use a class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.