AttributeError: 'list' object has no attribute 'extends'

Spelling mistake

It is extend rather than extends

Error demo:

1 In [27]: c = [2,3]                                                              
2 
3 In [28]: c.extends([5])                                                         
4 ---------------------------------------------------------------------------
5 AttributeError                            Traceback (most recent call last)
6 <ipython-input-28-2022e87158c8> in <module>
7 ----> 1 c.extends([5])
8 
9 AttributeError: 'list' object has no attribute 'extends'

debugging:

Since the error message saying that the object does not list extends this property, then we can first look at the list of attributes are what

Through line 42, you can see the list have extend property instead extends property

This is a known bug in the code typos

 1 In [29]: dir(list)                                                              
 2 Out[29]: 
 3 ['__add__',
 4  '__class__',
 5  '__contains__',
 6  '__delattr__',
 7  '__delitem__',
 8  '__dir__',
 9  '__doc__',
10  '__eq__',
11  '__format__',
12  '__ge__',
13  '__getattribute__',
14  '__getitem__',
15  '__gt__',
16  '__hash__',
17  '__iadd__',
18  '__imul__',
19  '__init__',
20  '__init_subclass__',
21  ',
'__iter__22  '__le__',
23  '__len__',
24  '__lt__',
25  '__mul__',
26  '__ne__',
27  '__new__',
28  '__reduce__',
29  '__reduce_ex__',
30  '__repr__',
31  '__reversed__',
32  ''__rmul__,
33  '__setattr__',
34  '__setitem__',
35  '__sizeof__',
36  '__str__',
37  '__subclasshook__',
38  'append',
39  'clear',
40  'copy',
41  'count',
42  'extend',
43  'index',
44  'insert',
45  'pop',
46  'remove',
47  'reverse',
48  'sort']

Other attributes are also several presentations about it

append append, everyone is familiar with the list of attributes. Note: You can only append a target

In [33]: c = [2,3]                                                              

In [34]: c.append(4)                                                            

In [35]: c                                                                      
Out[35]: [2, 3, 4]

 

clear clear the entire list

In [35]: c                                                                      
Out[35]: [2, 3, 4]

In [36]: c.clear()                                                              

In [37]: c                                                                      
Out[37]: []

 

a copy of the return copy list

In [38]: c = [2,3]                                                              

In [39]: b = c.copy()                                                           

In [40]: b                                                                      
Out[40]: [2, 3]

 

Statistics count the number of times specified element appears in the list

In [45]: c                                                                      
Out[45]: [2, 3, 2]

In [46]: c.count(2)                                                             
Out[46]: 2

 

a.extend (b) the b appended to the end of a note b should be. iterable objects , otherwise it will be reported TypeError: 'xxx' object is not iterable error

. 1 the In [49]: c.extend ([. 5 ])                                                          
 2  
. 3 the In [50 ]: C                                                                      
 . 4 Out [50]: [2,. 3, 2,. 5 ]
 . 5  
. 6  # if a dictionary is added, will dictionary key appended to the end of the dictionary 
. 7 the in [51 is]: a = { " a " :. 6 }                                                              
 . 8  
. 9 the in [52 is ]: c.extend (a)                                                            
 10  
. 11 the in [53 is]: c                                                                      
12 Out[53]: [2, 3, 2, 5, 'a']

 

Examples of errors:

 1 In [47]: c                                                                      
 2 Out[47]: [2, 3, 2]
 3 
 4 In [48]: c.extend(5)                                                            
 5 ---------------------------------------------------------------------------
 6 TypeError                                 Traceback (most recent call last)
 7 <ipython-input-48-5a85afdd21bf> in <module>
 8 ----> 1 c.extend(5)
 9 
10 TypeError: 'int' object is not iterable

 

Returns the index position of the specified element 1st appear in the list

The insert element is inserted into the location specified in the list

pop Removes the specified element from the list, return the removed elements

Remove to remove the elements specified in the list, no return value

sort the contents of the list is sorted, no return value

Guess you like

Origin www.cnblogs.com/kaerxifa/p/11077404.html