意外的Python类型错误:“list”对象不是callab

2024-05-03 12:15:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画两条线,每一条线分开时效果都很好,但当我一次画两条线时,就会有一个错误:

Traceback (most recent call last): File "2.py", line 99, in fspe2 = [fspe(t2,x) for t2 in t] TypeError: 'list' object is not callable

完整代码:

# -*- coding: utf-8 -*
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import math
from pylab import *

c = 2.998*10**10
hp = 6.626*10**-27
hb = 1.055*10**-27
kb = 1.381*10**-16
g = 6.673*10**-8
me = 9.109*10**-28
mp = 1.673*10**-24
q = 4.803*10**-10
sigT = 6.652*10**-25

p = 2.5
r014 = 1
E53 = 1
g42 = 1
delt12 =1
epsBR2 = 1
epseR1 = 1
DLG = 1 

r0 = r014*10**14
E0 = E53*10**53
g4 = g42*10**2.5
delt0 = delt12*10**12
epseR = epseR1*0.1
epsBR = epsBR2*0.01
n1 = 1.0
k = 0
DL = DLG*3.086*10**27

N0 = E0/(g4*mp*c**2)
SedL = (3*E0/(4*math.pi*n1*mp*c**2))**(1./3)
ttw = delt0/c  
ttn = SedL/(2*c*g4**(3./8))  
Reta = SedL/g4**(2./3)

def Gam3(t):
  if delt0 > SedL/(2*g4**(8./3)):
    return np.where(t<ttw,(SedL/delt0)**(3./8)*(4*t/ttw)**(-1./4),(SedL/delt0)**(3./8)*(4*ttw/ttw)**(-1./4)*(t/ttw)**(-7./16))
  else:
    return np.where(t<ttn,g4,g4*(t/ttn)**(-2./5))

def n3(t):
  if delt0 > SedL/(2*g4**(8./3)):
    return np.where(t<ttw,8*Gam3(t)**3*n1/g4,8*Gam3(ttw)**3*n1/g4*(t/ttw)**(-13./16))
  else:
    return np.where(t<ttn,7*n1*g4**2*(t/ttn)**-3,7*n1*g4**2*(t/ttn)**(-6./7))

def e3(t):
  if delt0 > SedL/(2*g4**(8./3)):
    return np.where(t<ttw,4*Gam3(t)**2*n1*mp*c**2,4*Gam3(ttw)**2*n1*mp*c**2*(t/ttw)**(-13./12))
  else:
    return np.where(t<ttn,4*g4**2*n1*mp*c**2,4*g4**2*n1*mp*c**2*(t/ttn)**(-8./7))

def Ne3(t):
  if delt0 > SedL/(2*g4**(8./3)):
    return np.where(t<ttw,N0*(t/ttw),N0)
  else:
    return np.where(t<ttn,N0*(t/ttn)**(3./2),N0)

gem = lambda t : epseR*e3(t)/(n3(t)*me*c**2)*(p-2)/(p-1)
BR  = lambda t : np.sqrt(8*math.pi*epsBR*e3(t))
gec = lambda t : 6*math.pi*me*c/(sigT*BR(t)**2*Gam3(t)*t)
num = lambda t : 3*q*BR(t)/(4*math.pi*me*c)*gem(t)**2*Gam3(t)
nuc = lambda t : 3*q*BR(t)/(4*math.pi*me*c)*gec(t)**2*Gam3(t)
Fmax = lambda t : Ne3(t)*math.sqrt(3)*q**3*BR(t)/(me*c**2)*Gam3(t)/(4*math.pi*DL**2)

def fspe(t,u):
  if num(t)<nuc(t):
    return np.where(u<num(t),(u/num(t))**(1./3)*Fmax(t),np.where(u<nuc(t),(u/num(t))**(-(p-1.)/2)*Fmax(t),(u/nuc(t))**(-p/2)*(nuc(t)/num(t))**(-(p-1.)/2)*Fmax(t)))*u
  else:
    return np.where(u<nuc(t),(u/muc(t))**(1./3)*Fmax(t),np.where(u<num(t),(u/nuc(t))**(-1./2)*Fmax(t),(u/num(t))**(-p/2)*(num(t)/nuc(t))**(-1.2)*Fmax(t)))*u

xmin = 2
xmax = 10
i = np.arange(xmin,xmax,0.01)
t = 10**i

plt.figure('God Bless: Lightcure')
plt.title(r'Lightcurve''\n2 Cases')
plt.xlabel(r'log t')
plt.ylabel(r'log Flux')

x = 10**10
fspe = [fspe(t1,x) for t1 in t] 
Lightcurve1 = [math.log10(a5) for a5 in fspe]
plt.plot(i,Lightcurve1,'.',label=r'$\nu=10^{10}$')

########################
## Strange thing happens!The above 4 sentences work well,
## why added the same statements, there's an Error?
x = 10**15
fspe2 = [fspe(t2,x) for t2 in t] 
### This place feedback--Traceback (most recent call last):
#  File "1.py", line 94, in <module>
#    fspe2 = [fspe(t2,x) for t2 in t] 
#TypeError: 'list' object is not callable
Lightcurve2 = [math.log10(a6) for a6 in fspe2]
plt.plot(i,Lightcurve2,'>',label=r'$\nu=10^{15}$')
#######################

plt.legend()
plt.grid(True)
plt.show()  

Tags: inreturnnppltmathwherenumttw
2条回答

在函数fspe上,在第91行使用一个列表进行赋值。在

fspe = [fspe(t1,x) for t1 in t]

我做了个简单的改变

^{pr2}$

至少是这样。我不知道它是否能达到你想要的效果。在

Traceback:从字面上讲,是一种将路径从错误消息追溯到导致错误消息的行的方法。Python的错误消息通常是非常好的,所以从表面上看它所说的内容,并按照它的指示进行操作。它甚至会描述产生这个结果的函数调用的路径,这些函数调用充当breadcrumb。在

  1. fspe2 = [fspe(t2,x) for t2 in t] TypeError: 'list' object is not callable
  2. 你在哪里打电话?该行中唯一的实例是fspe(t2,x)。在
  3. 它想打什么电话?一个list。因此,fspe在这一点上是list。在
  4. 让我们找出fspe的分配位置,在编辑器或IDE(或在这个浏览器窗口中)中搜索该术语。在
  5. def fspe(t,u):-不,它定义了一个函数。在
  6. fspe = [fspe(t1,x) for t1 in t]-这是我们的罪魁祸首!您创建了一个涉及先前定义的fspe函数的list,然后将其分配给相同的名称fspe,从而屏蔽了该函数并使其不可访问(并且,如果不再存在对它的引用,则完全由垃圾回收器删除)。在
  7. 修复方法是给它一个唯一的名称,比如fspe3或{}或{}。在

相关问题 更多 >