python程序报错Error:'int' object is not callable解决方法

写Python程序时出现了一个错误:
‘int’ object is not callable

程序本身是可以运行的,加了几行代码之后就报这个错了,我锁定在这几行代码上,找到了原因。

原因

报错 XXX is not callable时,是因为代码调用了一个不能被调用的变量或对象。具体而言,可能是调用了错误的函数和变量。易错情况就是函数和变量同名了,使得调用函数时,系统会误认为这是在调用变量,造成错误。
eg:

len = seq.__len__()//len是保存seq大小的变量
..................
..................
print len(seq)//len为系统提供的求list/dict等类型变量的大小的函数
             //此处会报错 is not collable,因为变量len与函数len同名

解决方法

  1. 找到冲突的变量名或函数名
  2. 改名字
发布了13 篇原创文章 · 获赞 8 · 访问量 5万+
展开阅读全文

scipy解非线性方程组报错TypeError: 'int' object is not callable

01-27

scipy解非线性方程组报错TypeError: 'int' object is not callable。网上说是参数和函数重名的缘故,但貌似我的并没有重名,代码如下: ``` from scipy.integrate import odeint import numpy as np import matplotlib.pyplot as plt from scipy.optimize import root,fsolve def fun(x): for i in range(0,len(x)): x[i] = float(x[i]) fy = np.array([(x[0]*2010**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2010**x[3])+(-75.2303)**2/(x[4]*2010**x[5])-6259)),(x[0]*2011**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2011**x[3])+(-75.2303)**2/(x[4]*2011**x[5])-5958)),(x[0]*2012**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2012**x[3])+(-81.4421)**2/(x[4]*2012**x[5])-2624)),(x[0]*2014**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2014**x[3])+(-75.2303)**2/(x[4]*2014**x[5])-4809)),(x[0]*2015**x[1])*np.exp(-1/2(39.701**2/(x[2]*2015**x[3])+(-84.2187)**2/(x[4]*2015**x[5])-3877)),(x[0]*2017**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2017**x[3])+(-81.4421)**2/(x[4]*2017**x[5])-7381))]) return fy res_fsolve = fsolve(fun,[1,1,1,1,1,1]) print(res_fsolve) ``` 报错内容如下: > TypeError Traceback (most recent call last) > <ipython-input-18-0f6b923ff468> in <module> > 9 fy = np.array([(x[0]*2010**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2010**x[3])+(-75.2303)**2/(x[4]*2010**x[5])-6259)),(x[0]*2011**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2011**x[3])+(-75.2303)**2/(x[4]*2011**x[5])-5958)),(x[0]*2012**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2012**x[3])+(-81.4421)**2/(x[4]*2012**x[5])-2624)),(x[0]*2014**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2014**x[3])+(-75.2303)**2/(x[4]*2014**x[5])-4809)),(x[0]*2015**x[1])*np.exp(-1/2(39.701**2/(x[2]*2015**x[3])+(-84.2187)**2/(x[4]*2015**x[5])-3877)),(x[0]*2017**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2017**x[3])+(-81.4421)**2/(x[4]*2017**x[5])-7381))]) > 10 return fy > ---> 11 res_fsolve = fsolve(fun,[1,1,1,1,1,1]) > 12 print(res_fsolve) > > c:\users\anton\appdata\local\programs\python\python36\lib\site-packages\scipy\optimize\minpack.py in fsolve(func, x0, args, fprime, full_output, col_deriv, xtol, maxfev, band, epsfcn, factor, diag) > 145 'diag': diag} > 146 > --> 147 res = _root_hybr(func, x0, args, jac=fprime, **options) > 148 if full_output: > 149 x = res['x'] > > c:\users\anton\appdata\local\programs\python\python36\lib\site-packages\scipy\optimize\minpack.py in _root_hybr(func, x0, args, jac, col_deriv, xtol, maxfev, band, eps, factor, diag, **unknown_options) > 211 if not isinstance(args, tuple): > 212 args = (args,) > --> 213 shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,)) > 214 if epsfcn is None: > 215 epsfcn = finfo(dtype).eps > > c:\users\anton\appdata\local\programs\python\python36\lib\site-packages\scipy\optimize\minpack.py in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) > 24 def _check_func(checker, argname, thefunc, x0, args, numinputs, > 25 output_shape=None): > ---> 26 res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) > 27 if (output_shape is not None) and (shape(res) != output_shape): > 28 if (output_shape[0] != 1): > > <ipython-input-18-0f6b923ff468> in fun(x) > 7 for i in range(0,len(x)): > 8 x[i] = float(x[i]) > ----> 9 fy = np.array([(x[0]*2010**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2010**x[3])+(-75.2303)**2/(x[4]*2010**x[5])-6259)),(x[0]*2011**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2011**x[3])+(-75.2303)**2/(x[4]*2011**x[5])-5958)),(x[0]*2012**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2012**x[3])+(-81.4421)**2/(x[4]*2012**x[5])-2624)),(x[0]*2014**x[1])*np.exp(-1/2(39.9612**2/(x[2]*2014**x[3])+(-75.2303)**2/(x[4]*2014**x[5])-4809)),(x[0]*2015**x[1])*np.exp(-1/2(39.701**2/(x[2]*2015**x[3])+(-84.2187)**2/(x[4]*2015**x[5])-3877)),(x[0]*2017**x[1])*np.exp(-1/2(41.3866**2/(x[2]*2017**x[3])+(-81.4421)**2/(x[4]*2017**x[5])-7381))]) > 10 return fy > 11 res_fsolve = fsolve(fun,[1,1,1,1,1,1]) > > TypeError: 'int' object is not callable 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览