gpt4 book ai didi

python - NameError:名称 'first' 未定义

转载 作者:行者123 更新时间:2023-12-01 04:00:52 25 4
gpt4 key购买 nike

我是Python新手,我正在尝试绘制函数图。这是我的代码:

class Optic():
def __init__(self):
'''
param define
'''
global time,lamda, light_speed, Fc, D, z, beta, T0
self.__time = np.linspace(-400,400, 1600) #Picosec
self.__lamda = 1550000 #Picometer
self.__light_speed = 0.3 #meter/Picosec
self.__Fc=light_speed/lamda #Hz
self.__D = 17*np.power(10,-6) #sec/meter
self.__z=[0,10000,20000,30000] #meter
self.__beta = (-(np.power(lamda,2)/(2*np.pi*light_speed))*D) #sec/meter
self.__T0 = 200
# time = np.linspace(-400*np.power(10,-12), 0.5*np.power(10,-12), 400*np.power(10,-12)) #sec
# lamda = 1550*np.power(10,-9) #meter
# light_speed = 3*np.power(10,8) #sec/meter
# Fc=light_speed/lamda #Hz
# D = 17*np.power(10,-6) #sec/meter
# z=[0,10000,20000,30000] #meter
# beta = (-(np.power(lamda,2)/(2*np.pi*light_speed))*D) #sec/meter
# T0 = 20*np.power(10,-12)

def firstPulse(self):
global first
first = np.exp(-0.5*np.power(time/T0,2))

def main():
plt.plot(first, time)
plt.show()


if (__name__ == "__main__"):
main()

但我得到:

NameError: name 'first' is not defined

顺便说一句,我正在使用 Spyder。

最佳答案

您已初始化 Optic 并调用定义该变量的 def firstPulse(self)。

类似这样的事情:

def main():
opt = Optic():
opt.firstPulse()
plt.plot(first, time)
plt.show()

关于python - NameError:名称 'first' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36570633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com