简体   繁体   中英

How to call a python variable from another script's function?

I can call a module from another script with from myFile import myModule but what if the variable I want is inside a function of the said module?

I want to get a list called varlist from this FLASK module below...

@app.route('/DCF-calculator', methods =['POST'])

def DCFcalc():         
    n = request.form['Yrityksennimi'];
    g = request.form['Ennustettukasvu'];
    dr = request.form['Diskonttokorko'];
    gdp = request.form['BKT'];
    s = request.form['Osakkeidenyhteenlaskettumaara'];
    f = request.form['Vapaakassavirta'];
    v = request.form['Mittausajanpituus'];
    varlist = [n, g, dr, gdp, s, f, v]
    print(varlist)
    return json.dumps({'status': 'OK', 'YN':n, 'kasvu':g, 'DK':dr, 'BK':gdp,'OM':s,'KV':f,'MP':v});

and then somehow import it to another script which is supposed to do all the math between those inputs.

You can't import just a variable from within a function in a module into another script.

What you could do is, add a getter property function to the first module which would return the variable value.

Do something like this in your first module:

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        print("getter of x called")
        return self._x

Check out: properties in python

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