[Tutor] longest common substring

Dave Angel d at davea.name
Sat Nov 12 14:22:16 CET 2011


On 11/12/2011 03:54 AM, lina wrote:
> <SNIP>
> The one I tried :
>                  if longest>= 2:
>                      sublist=L1[x_longest-longest:x_longest]
>                      result=result.append(sublist)
>                      if sublist not in sublists:
>                           sublists.append(sublist)
>
> the $ python3 CommonSublists.py
> atom-pair_1.txt atom-pair_2.txt
> Traceback (most recent call last):
>    File "CommonSublists.py", line 47, in<module>
>      print(CommonSublist(a,b))
>    File "CommonSublists.py", line 24, in CommonSublist
>      result=result.append(sublist)
> AttributeError: 'NoneType' object has no attribute 'append'
>
> in local domain I set the result=[]
> I don't know why it complains its NoneType, since the "result" is
> nearly the same as "sublists".
>
Assuming this snippet is part of a loop, I see the problem:

result  = result.append(sublist)

list.append() returns none.  It modifies the list object in place, but 
it doesn't return anything.  So that statement modifies the result 
object, appending the sublist to it, then it sets it to None.  The 
second time around you see that error.

In general, most methods in the standard library either modify the 
object they're working on, OR they return something.   The append method 
is in the first category.


-- 

DaveA



More information about the Tutor mailing list