如果list变量和list函数重名,会有什么后果呢?我们可以参考如下代码:

list = ['泡芙', '汤圆', '鱼儿', '骆驼']

tup_1 = (1, 2, 3, 4, 5)
tupToList = list(tup_1)

print(tupToList)

代码运行后出错了,出错原因是TypeError: 'list' object is not callable

Traceback (most recent call last):
  File "D:/python_workshop/python6/lesson3_list.py", line 6, in <module>
    tupToList = list(tup_1)
TypeError: 'list' object is not callable

callable()是python的内置函数,用来检查对象是否可被调用,可被调用指的是对象能否使用()括号的方法调用

在如上代码中,由于变量list和函数list重名了,所以函数在使用list函数时,发现list是一个定义好的列表,而列表是不能被调用的,因此抛出一个类型错误

解决办法:我们只需修改变量名list就可以了:

list_1 = ['泡芙', '汤圆', '鱼儿', '骆驼']

tup_1 = (1, 2, 3, 4, 5)
tupToList = list(tup_1)

print(tupToList)

运行后和结果是正常的:

[1, 2, 3, 4, 5]

因此,在命名变量时要注意,应避免和python的函数名、关键字冲突。

解决:TypeError: 'list' object is not callable的更多相关文章

  1. 解决Flask和Django的错误“TypeError: 'bool' object is not callable”

    跟着欢迎进入Flask大型教程项目!的教程学习Flask,到了重构用户模型的时候,运行脚本后报错: TypeError: 'bool' object is not callable 这是用户模型: c ...

  2. appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!

    这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...

  3. flask渲染模板时报错TypeError: 'UnboundField' object is not callable

    渲染模板时,访问页面提示TypeError: 'UnboundField' object is not callable 检查代码,发现实例化表单类是,没有加括号:form = NewNoteForm ...

  4. Python: TypeError: 'dict' object is not callable

    问题:  TypeError: 'dict' object is not callable 原因:  dict()是python的一个内建函数,如果将dict自定义为一个python字典,在之后想调用 ...

  5. python -- TypeError: 'module' object is not callable

    文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...

  6. python Flask :TypeError: 'dict' object is not callable

    flask 基于Werkzeug .. @moudule.route('/upload', methods=['GET', 'POST']) def upload_file(): global _fl ...

  7. TypeError: 'module' object is not callable cp fromhttp://blog.csdn.net/huang9012/article/details/17417133

    程序代码  class Person:      #constructor      def __init__(self,name,sex):           self.Name = name   ...

  8. TypeError: 'QueryDict' object is not callable

    id = int(request.POST('id')) Error message: TypeError: 'QueryDict' object is not callable Error rese ...

  9. Python学习笔记1 -- TypeError: 'str' object is not callable

    Traceback (most recent call last): File "myfirstpython.py", line 39, in <module> pri ...

随机推荐

  1. h5 localStorage本地存储

    用户名:<input type="text" id="txtname"/> 密码:<input type="text" i ...

  2. 2014-08-28——Android和IOS的简单嗅探,以及横竖屏的捕获思路

    一般通过navigator.userAgent来嗅探Android系统和IOS系统: if(/android/i.test(navigator.userAgent)){ //android } if( ...

  3. 22个所见即所得在线Web编辑器

    这些 Web 编辑器可以在线编辑和处理富 Web 内容,包括格式文本,表格,图片,媒体,链接等等,非常适合集成到 CMS网站内容管理系统中使用.本文又搜集了 22 个 Web 在线编辑器,它们基本代表 ...

  4. Django报:AttributeError: tuple object has no attribute get

    def index(request): hero_list=models.HeroInfo.objects.all() return render_to_response('index.html',{ ...

  5. 检测tomcat服务是否正常

    由于tomcat服务经常会出现进程在,但是服务却无法正常响应的问题,而且进程跑在docker容器中,使用zabbix控制不是很方便,故此写了个简单的小脚本: #!/bin/bash #Author:f ...

  6. corethink功能模块探索开发(四)让这个模块跑起来

    让这个模块跑起来,太费劲了,多半原因是自己太粗心,opencmf.php中“uid”写成了“pid”,de了好几天的bug也没有搞出来,又加上最近发生了些事(brokenhearted)... 上报错 ...

  7. Python traps and pitfalls

    @1: >>> def func(a, L=[]): ... L.append(a) ... print(L) ... >>> func(10) [10] > ...

  8. STL之map、set灵活使用

    1.LA 5908/UVA1517 Tracking RFIDs 题意:给出s个传感器的位置,以及其感应范围.如果某个方向上有墙,则该方向上感应距离减1.现在有w个墙,给出p个物品的位置,问其能被几个 ...

  9. JavaScript:学习笔记(6)——New运算符

    JavaScript:学习笔记(6)——New运算符 new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例. 快速开始 当你使用new关键字的时候,会 创建一个新的对象 将th ...

  10. 0530JavaScript基础2

    常用内置对象 所谓内置对象就是ECMAScript提供出来的一些对象,我们知道对象都是有相应的属性和方法 数组Array(部分相当于列表) 1.数组的创建方式 var colors = ['red', ...