AttributeError: 'tuple' object has no attribute 'extend'
出错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'的更多相关文章
- AttributeError: 'NoneType' object has no attribute 'extend'
Python使用中可能遇到的小问题 AttributeError: 'NoneType' object has no attribute 'extend' 或者AttributeError: 'Non ...
- Django报:AttributeError: tuple object has no attribute get
def index(request): hero_list=models.HeroInfo.objects.all() return render_to_response('index.html',{ ...
- AttributeError: 'list' object has no attribute 'extends' && list详解
拼写错误 是extend 而不是extends 出错demo: In [27]: c = [2,3] In [28]: c.extends([5]) ------------------------ ...
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...
- AttributeError: 'list' object has no attribute 'write_pdf'
我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...
- attributeError:'module' object has no attribute ** 解决办法
写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...
- AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'
/*************************************************************************** * AttributeError: 'modu ...
- AttributeError: 'dict_values' object has no attribute 'translate'
/***************************************************************************************** * Attribu ...
- python3 AttributeError: 'NoneType' object has no attribute 'split'
from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...
随机推荐
- POJ3468:A Simple Problem with Integers (线段树||树状数组||Splay解决基本问题的效率对比)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- bzoj1007 [HNOI2008]水平可见直线——单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1007 可以把直线按斜率从小到大排序,用单调栈维护,判断新直线与栈顶的交点和栈顶与它之前直线的 ...
- appium学习【三】:截图时,图片命令中包含当前的函数名,以区分错误是在哪个函数报的
import sys funcName = sys._getframe().f_back.f_code.co_name #获取调用函数名 print sys._getframe().f_code.co ...
- zoj 3861(dfs)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
- vue 子组件 索引 ref
尽管有props和events,但是有时任然需要在javascript中直接访问子组件.为此可是使用ref为子组件指定一个索引ID. <div id="parent"> ...
- 从0开始学习Hadoop(2)安装JDK以及设置SSH
安装JDK 使用ppa/源方式安装 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracl ...
- ubuntu12.04中极品五笔简体繁体切换
不小心切换成了繁体,按Ctrl+:,可以切换回来.
- spring分布式事务学习笔记(1)
此文已由作者夏昀授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 分布式事务对性能有一定的影响,所以不是最佳的解决方案,能通过设计避免最好尽量避免. 分布式事务(Distrib ...
- Ocelot(八)- Ocelot不支持的几个方面
不支持 Ocelot不支持的几个方面 Chunked Encoding - Ocelot将始终获取正文大小并返回Content-Length标头.很抱歉,如果这对您的用例不起作用! 转发主机标头 - ...
- 在代码里更新autolayout布局
//遍历view约束(高,宽) NSArray* constrains = self.View.constraints; for (NSLayoutConstraint* constraint in ...