为了账号安全,请及时绑定邮箱和手机立即绑定

显示TypeError: 'list' object is not callable,这是什么原因?

显示TypeError: 'list' object is not callable,这是什么原因?

慕桂英546537 2022-04-21 15:15:15
class test:def list(self):list = []return listprint test().list()运行结果:[]但如果这样:class test:def __init__(self):self.list=[]def list(self):list = []return listprint test().list()就会报错,像这样:Traceback (most recent call last):File "C:\Users\pqj123\Desktop\test.py", line 7, in <module>print test().list()TypeError: 'list' object is not callable这是什么原因,求指教。
查看完整描述

2 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

error已经告诉你了 list对象不能用函数形式调用,就是不能callable。
简单来说就是list变量和list函数重名了。

查看完整回答
反对 回复 2022-04-24
?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

>>> list('asdf')
Traceback (most recent call last):
File "<pyshell#154>", line 1, in <module>
list('asdf')
TypeError: 'list' object is not callable
你可以先确认一下list变量是否被使用:

>>> list #输入list回车,检查list的值
[1, 3, 2, 4, 6, 7, 12, 10] #发现list被占用
>>> del list #删除变量
>>> list #确认一下结果
<class 'list'> #变量被删除
>>> list('adfadf') #再次调用list()函数
['a', 'd', 'f', 'a', 'd', 'f'] #可以正常使用了。



查看完整回答
反对 回复 2022-04-24
  • 2 回答
  • 0 关注
  • 712 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号