如果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实现缓存ajax请求数据

    使用背景:要实现每次鼠标hover“能力雷达”,则显示能力雷达图(通过ajax请求数据实现雷达图数据显示),所以每次hover都去请求ajax会影响性能,因此这里要用到本地缓存. 实现: 此处是通过传 ...

  2. zookeeper单机伪集群配置

    一.配置 1.在 opt 目录下建一个文件夹 zk,分别把zookeeper 安装包复制三份,命令为zookeeper-0  zookeeper_1  zookeeper_2 2.分别在每一个zook ...

  3. Version 1.5 of the JVM is not suitable for this product.Version:1.6 or greater is required

    近期在公司涉及到了服务器等的扩展,smartfoxserver扩展使用的Eclipse,尽管没学过java.可是咱毕竟是C++起价的,其它语言看看也就会了,项目依然做着,近期看到某同学有一些java的 ...

  4. SaltStack远程执行shell脚本

    编辑文件fansik.sh 脚本内容: #!/bin/bash # Author: fansik # data: 2017年 09月 26日 星期二 :: CST touch /tmp/fansik. ...

  5. Python 9 sqlalchemy ORM

    一.ORM介绍: orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...

  6. PHP生成缩略图,控制图片质量,支持.png .jpg .gif

    namespace common\components; class ResizeImageHelper { public $type;//图片类型 public $width;//实际宽度 publ ...

  7. 【HackerRank】 The Full Counting Sort

    In this challenge you need to print the data that accompanies each integer in a list. In addition, i ...

  8. pd.read_csv的header用法

    默认Header = 0: In [3]: import pandas as pd In [4]: t_user = pd.read_csv(r'C:\Users\Song\Desktop\jdd_d ...

  9. 记Outlook插件与Web页面交互的各种坑 (含c# HttpWebRequest 连接https 的完美解决方法)

    1) 方案一,  使用Web Service  基础功能没问题, 只是在连接https (ssh) 网站时, 需要针对https进行开发 (即http 和https 生成两套接口, 不太容易统一 ). ...

  10. windows环境下安装部署并启用zkui的web图形界面

    在此之前的工作:不是本机部署的三个服务器最为伪集群的zookeeper环境,并将三个为服务启动起来. 然后才有了下面的工作. 1. 首先,zkui项目地址:https://github.com/Dee ...