来自另一个python模块的函数如何在另一个定义的函数中使用?

在这里,我想在已定义的函数f(v)中使用模块FrenselIntegral.py中的c(v)和s(v)。

import FrenselIntegral as fi

v=0.0
u=0.2

def f(v): 0.5*((0.5-fi.c(v))**2+(0.5-fi.s(v))**2)

file=open("Straightedge diffraction pattern.txt","w")

for i in range (25):
   print>>file,v,f(v)
   v=v+u
file.close()

但是,输出是

Traceback (most recent call last):
  File "c:/Users/Shubhadeep/Desktop/New folder/Straightedge diffraction pattern.py", line 11, in <module>
    print>>file,v,f(v)
  File "c:/Users/Shubhadeep/Desktop/New folder/Straightedge diffraction pattern.py", line 4, in f
    def f(v): 0.5*((0.5-fi.c(v))**2+(0.5-fi.s(v))**2)
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'
评论
xet
xet

该点看起来像fi.c(v)返回None。您可以检查v的哪些值给出该结果,或者只是避免返回None,例如:

import FrenselIntegral as fi

v=0.0
u=0.2

def f(v):
  if fi.c(v) is None or fi.s(v) is None:
    return 0
  else:
    return 0.5*((0.5-fi.c(v))**2+(0.5-fi.s(v))**2)

file=open("Straightedge diffraction pattern.txt","w")

for i in range (25):
   print>>file,v,f(v)
   v=v+u
file.close()
点赞
评论