出错demo

 In [5]: a.extend([4,5])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-42b5da5e2956> in <module>
----> 1 a.extend([4,5]) AttributeError: 'tuple' object has no attribute 'extend'

打印一下tuple类型的属性可以看到,tuple类型除内置类型外,只有count和index两个属性

extend是list类型的方法

 In [3]: dir(tuple)
Out[3]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']

extend是1个列表+另一个列表,

它会改变原来列表的长度,而不会生成一个新的列表

所以,如果你使用了a=list.extend(XXX),它并不会如你希望 返回一个列表,而是一个None

示例 :

 In [6]: b=[1,2]                                                                 

 In [7]: c=b.extend([3])                                                         

 In [8]: b
Out[8]: [1, 2, 3] In [10]: print(c)
None In [11]: type(c)
Out[11]: NoneType

顺便说明一下:

count表示统计元组中指定的1个元素 出现的次数

 In [20]: b=(2,2,2,3,4)                                                          

 In [21]: b.count(2)
Out[21]: 3

index返回指定元素第1次出现的位置(下标)

 In [20]: b=(2,2,2,3,4)                                                          

 In [22]: b.index(3)
Out[22]: 3 In [23]: b.index(2)
Out[23]: 0

附:

list的所有属性如下

 In [4]: dir(list)
Out[4]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']

AttributeError: 'tuple' object has no attribute 'extend'的更多相关文章

  1. AttributeError: 'NoneType' object has no attribute 'extend'

    Python使用中可能遇到的小问题 AttributeError: 'NoneType' object has no attribute 'extend' 或者AttributeError: 'Non ...

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

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

  3. AttributeError: 'list' object has no attribute 'extends' && list详解

    拼写错误 是extend  而不是extends 出错demo: In [27]: c = [2,3] In [28]: c.extends([5]) ------------------------ ...

  4. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  5. AttributeError: 'list' object has no attribute 'write_pdf'

    我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...

  6. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

  7. AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'

    /*************************************************************************** * AttributeError: 'modu ...

  8. AttributeError: 'dict_values' object has no attribute 'translate'

    /***************************************************************************************** * Attribu ...

  9. python3 AttributeError: 'NoneType' object has no attribute 'split'

    from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...

随机推荐

  1. 如何完成dedecms外部数据库调用|跨数据库数据调用

    第1步:打开网站include\taglib文件夹中找到sql.lib.php文件,并直接复制一些此文件出来,并把复制出来的这个文件重命名为mysql.lib.php. 注:mysql.lib.php ...

  2. Ubuntu linux 返回上一次访问的目录

    cd - (cd空格 减号)返回最近一次访问的目录 这个非常方便.平时经常用终端切换目录,能够方便地回到原来的目录就很爽了. jiqing@jiqing-pad:/usr/local/redis/sr ...

  3. JAVA web简单的登录界面jsp实现

    此次试验所用到的软件是myeclipse10,tomcat7,Dreamweaver,sqlserver2008数据库.可以实现用户使用用户名和密码登录.如果登录成功,页面会显示登录成功,如果密码错误 ...

  4. Overview of MIDI

    东拼西凑的介绍 MIDI which means Musical Instrument Digital Interface, introduced in 1980's provided a inter ...

  5. 基于《Hadoop权威指南 第三版》在Windows搭建Hadoop环境及运行第一个例子

    在Windows环境上搭建Hadoop环境需要安装jdk1.7或以上版本.有了jdk之后,就可以进行Hadoop的搭建. 首先下载所需要的包: 1. Hadoop包: hadoop-2.5.2.tar ...

  6. UVa 12716 && UVaLive 6657 GCD XOR (数论)

    题意:给定一个 n ,让你求有多少对整数 (a, b) 1 <= b <= a 且 gcd(a, b) = a ^ b. 析:设 c = a ^ b 那么 c 就是 a 的约数,那么根据异 ...

  7. 网站SEO优化如何让百度搜索引擎绝的你的网站更有抓取和收录价值呢?_孙森SEO

    今天孙森SEO为大家唠唠网站到底该如何优化才会让百度搜索引擎绝的你的网站更有抓取和收录价值呢? 第一方面:网站创造高品质的内容,可以为用户提供独特的价值. 1.百度作为搜索引擎,网站内容必须满足 搜索 ...

  8. Centos 7 chrome

    share from https://www.cnblogs.com/lenmom/p/9195581.html 1. 下载Chrome浏览器的rpm包 https://www.chrome64bit ...

  9. 版本管理工具SVN的使用

    一.安装服务器端和客户端 需要的软件: 1.服务器端:SlikSVN,下载传送门:http://www.sliksvn.com/en/download/ 2.客户端:“乌龟”,下载传送门:http:/ ...

  10. icons使用

    1.将选中图标加入项目 2.unicode方式查看连接在线连接 3.复制代码到样式表 4.引用样式,并设置I标签,颜色和大小可以通过设置i标签color和font-size进行调整 <i cla ...