出错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. 【OpenFOAM】——OpenFOAM入门算例学习

    1  明确目标——为啥费老大劲儿学习OpenFOAM 学习OpenFOAM主要出于课题需要,希望实现以下几个目标: l  [ ]学会用SnappyHexMesh生成高质量网格: l  [ ]学习使用O ...

  2. bzoj4631

    4631: 踩气球 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 260  Solved: 133[Submit][Status][Discuss] ...

  3. 【183】VMware + CentOS 安装教程等

    目录: VMware 虚拟机安装 虚拟机上安装 CentOS VMware Tools 安装 为程序增加环境变量 其他 1. VMware 虚拟机安装   ※ 参考:VMware 虚拟机安装使用教程( ...

  4. 返回一个集合对象,同时这个集合的对象的属性又是一个集合对象的处理方法(ViewModel)

    如果遇到需要返回一个集合对象,其中该集合中的属性又是一个集合.第一种:可在考虑用外键关联,比如在控制器中可以采用预先加载的方式加载关联的数据,比如 RoleManager.Roles.Include& ...

  5. asp.net 常用代码

    asp.net 下拉菜单选中 ddlCity.SelectedIndex = ddlCity.Items.IndexOf(ddlCity.Items.FindByValue(")); 关于. ...

  6. linux更改用户名,域名(转载)

    转自:http://huangro.iteye.com/blog/365975 1. 我们在root权限下,使用命令: usermod -l new_user_name old_user_name 即 ...

  7. Ruby Regexp类

    正则表达(Regexp)类 更新:2017/06/18 改变[]集合的表格大小 80% ---> 100%  定义 正则表达: 和字符串匹配的模式(pattern)的写法 正则表达(Regexp ...

  8. 记bugku的——“welcome to bugkuctf”

    今天终于拾起来ctf的比赛了,开始了练习之旅.今天写一道bugku上的题目wp,属于利用php源码泄漏的题目吧,我觉得不是很简单...所以把自己的思路放上来. 题目源头:http://120.24.8 ...

  9. 51nod 1237 最大公约数之和 V3【欧拉函数||莫比乌斯反演+杜教筛】

    用mu写lcm那道卡常卡成狗(然而最后也没卡过去,于是写一下gcd冷静一下 首先推一下式子 \[ \sum_{i=1}^{n}\sum_{j=1}^{n}gcd(i,j) \] \[ \sum_{i= ...

  10. Java Genericity

    四.  Java Genericity 1.  Genericity 泛型 泛型 <T> 1. 泛型就是参数化类型 2. 作用:安全,方便 3. 适用于对多种数据类型执行相同功能的代码,主 ...