简体   繁体   English

如何在另一个 Python 文件中使用另一个 function 中的 function 的计算值?

[英]How can I use a calculated value of a function in another function in another Python file?

I have wrote the following code in derivation.py :我在derivation.py中编写了以下代码:

def Interpolation(ableitungWinkel,x_values):
    
    z = medfilt(derivation,3)
    diff = abs(derivation-z) 
    new_smootheddata = np.where(diff>3,z,derivation)
    x=np.arange(0,len(x_values[:-2]))    
    f = interp1d(x,new_smootheddata,kind="linear")   
    xnew = np.arange(0, len(x_values[:-3]),0.01)
    ynew = f(xnew)
    s=plt.plot(x, z,"o",xnew, ynew, "-")
    
    return s

In my project there is also integration.py .在我的项目中还有integration.py In this Python file I need the values which z calculates in the function def interpolation for this calculation:在这个 Python 文件中,我需要z在 function def interpolation中计算的值用于此计算:

def horizontalAcceleration(strideData):
    resultsHorizontal = list()

    for i in range (len(strideData)):
        yAngle = z
        xAcceleration = strideData.to_numpy()[i, 4]
        yAcceleration = strideData.to_numpy()[i, 5]
        
        a = ((m.cos(m.radians(yAngle)))*yAcceleration)-((m.sin(m.radians(yAngle)))*xAcceleration) 

        resultsHorizontal.append(a)

    resultsHorizontal.insert(0, 0)
    
    return resultsHorizontal

As you can see I have already added z to the function def horizontalAcceleration at the place where it should go.如您所见,我已经在 go 的位置将 z 添加到 function def HorizontalAcceleration 中。 To use z there, I tried the following: from derivation import z要在那里使用 z,我尝试了以下方法: from derivation import z

But that doesn't work.但这不起作用。 Because then I get the error: ImportError: cannot import name 'z' from 'derivation'因为那时我得到了错误: ImportError: cannot import name 'z' from 'derivation'

Have anybody an idea how I can solve this problem?有人知道我该如何解决这个问题吗? Thanks for helping me.谢谢你帮助我。

I think that your misunderstanding is because you think a function is like a script that has been run and modified a.global state.我认为您的误解是因为您认为 function 就像一个脚本,已经运行并修改了 a.global state。 That's not what a function is.这不是 function 是什么。 A function is a series of actions performed on its inputs (ignoring closures for a minute) which returns some results. function 是对其输入执行的一系列操作(忽略一分钟的闭包),返回一些结果。 You can call it many times, but without calling it, it never executes.您可以多次调用它,但不调用它,它永远不会执行。 Once it stops executing all its variables go out of scope.一旦它停止执行 scope 之外的所有变量 go。

You can import and call a function though.不过,您可以导入并调用 function。 So you can change the return type of Interpolation to return everything you need somewhere else.因此,您可以更改 Interpolation 的返回类型以在其他地方返回您需要的所有内容。 Eg例如

def Interpolation(...):
...
return {'z': z, 's': s}

Then somewhere you import that function, call it, get back all the data you need, then pass that to your other function.然后在某个地方导入 function,调用它,取回您需要的所有数据,然后将其传递给您的其他 function。

import Interpolation from derivation
# get z and s in a dict result
result = Interpolation(...)
# pass s as well as the other argument to your other function
horizontalAcceleration(strideData, result['s'])

暂无
暂无

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

相关问题 如何在python的另一个应用程序中使用函数返回的值? - How can I use a value returned by a function in another application in python? 如何在 Python 3 中获取一个函数以返回我可以在另一个函数中使用的值? - How can I get a function in Python 3 to return a value that I can use in another function? 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 当 python 没有返回时,如何访问另一个 function 中的值? - How can I access value in another function when there is no return at python? 如何保存随机输出以在 python 的另一个函数中使用它 - How can I save the random output to use it in another function in python 如何在函数Login()中获取'username'的值,以在另一个python程序上使用它? - How can I get the value of 'username' inside the function Login() to use it on another python program? 如何在另一个python文件中使用函数 - How to use a function in another python file 如何在python中使用另一个文件中的函数 - how to use function from another file in python 如果函数返回用户输入的值,如何在不要求用户输入另一个值的情况下在另一个函数中使用该值 - If a function returns a value entered by a user, how can I use that value in another function without asking the user to input another value 无法使用另一个函数/ python文件中的返回值 - Cant use a return value from another function/python file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM