简体   繁体   English

如何确定要在python中定义的函数使用哪个变量

[英]How can I determine which variable to use for a defined function in python

I have defined this function : 我已经定义了这个功能:

def xintegral(x,k,z,xdot):
     f=np.exp((x-xdot)**2*k/(2*z)*1j)
     return f

And I want to insert this function in the place of func to this process: 我想在此过程中的func位置插入此函数:

def simpson(func,mini,maxi,N): 
     sum=0   
     h=(maxi-mini)/N 
     even=mini+2*h
     odd=mini+h 

     for i in range(N//2-1):  
          sum+=func(even)*2
          even+=2*h
     for i in range(N//2): 
          sum+=func(odd)*4
          odd+=2*h
     sum=(sum+(func(mini)+func(maxi)))*h/3 
     return sum

I want to use as a func the xintegral with variable the xdot. 我想将xdot变量用作xintegral函数。 So I tried to write it like that : simpson(xintegral(x,k,z,xdot),mini,maxi,N) but I want the variable to be the xdot. 所以我试图这样写:simpson(xintegral(x,k,z,xdot),mini,maxi,N),但我希望变量是xdot。 The variables odd,mini,maxi can replace xdot? 变量奇数,最小,最大可以代替xdot吗? How can I do that? 我怎样才能做到这一点?

You can do this with partial application. 您可以使用部分应用程序执行此操作。 This will create a function p_xintegral that will take a single argument. 这将创建一个函数p_xintegral ,该函数将使用单个参数。 That single argument will be assigned to xdot with in xintegral and x , y , and z are determined at the time you do the partial application: 该单个参数将在xintegral分配给xdot ,并且xyz在执行部分应用程序时确定:

from functools import partial

p_xintegral = partial(xintegral, x, y, z)

So you should then be able to call simpson(p_xintegral, mini, maxi, N) . 因此,您应该能够调用simpson(p_xintegral, mini, maxi, N)

I think this is what you want. 我想这就是你想要的。 Otherwise your f is not a function 否则你的f不是函数

def xintegral(x,k,z,xdot):
     f=lambda j:np.exp((x-xdot)**2*k/(2*z)*1j)
     return f

暂无
暂无

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

相关问题 如何使用先前在循环(列表)中定义的变量来实现单独的功能? - How can I use a variable that I previously defined in a loop (which is a list) for a separate function? 在python类中,如何在另一个函数中使用一个函数定义的变量? - In python class, how can I use the variable defined by one function in another function? 如何访问在 Python 中的 function 之后定义的变量? - How can I access a variable that is defined after the function in Python? 确定从哪个文件中定义了一个函数在 python 中 - determine from which file a function is defined in python Python:如何编写 function 以确定 dataframe 中的哪个变量与指定列的绝对相关性最高? - Python: How do I write a function to determine which variable in a dataframe has the highest absolute correlation with a specified column? 如何在Python中调用带有存储为字符串的变量的函数 - How in Python can I call a function with a variable which is store as a string 如何使用 Python (boto3) 来确定哪些 EC2 实例具有 CloudWatch 代理? - How can I use Python (boto3) to determine which EC2 instances have CloudWatch agents? 如何在python中使用嵌套字典返回用户定义的变量 - how to use nested dictionary in python which return user defined variable 如何从另一个重新定义的 python 文件中调用变量 - How can I call a variable from another python file which is re-defined 在python中,如何从导入模块中定义的函数中使用/操作脚本中定义的对象? - In python how can I use/manipulate an object defined in a script from a function defined in an imported module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM