[Tutor] A Pythonic LinkedList Implementation

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Tue Mar 29 03:58:30 CEST 2005


Well, my opinion about your code :

At first, it seems far too complex for what it is ! First, there is a 
whole lot of useless tests ! For example :

if node is not None and type(node) == list

The "node is not None" is useless, because the type of None is ... 
NoneType so type(node) == list is enough to ensure what you want. And 
you have a lot of tests like that.

Then, you code is still buggy ! Just try :

l = LinkedList([1,2,3])
l < 4
l == 4

with code like :

   def __eq__(self, y):
      if [...]
      try: [...]
      except:
         return self == y

that's jsut natural ...
remember that "self == y" will just call ... self.__eq__ !
Thus you get an infinite loop !!!

Another thing is your indentation. Please, always use the same 
indentation level everywhere ! Don't just align code that are not at the 
same level ... that reduces the readability of your code. I think the 
worst part was some code like :

if foo:
          line of code
          line of code
          line of code
          line of code
          line of code
          line of code
          line of code
          line of code
else:    line of code
another line of code

... in your __init__ method. Please, never do that again ! I almost 
missed the "else" !

At last, if you use linked list why embed a python list in it ???????
I can think of applications for a linked list, but if you embed a real 
list, what is the use of your links ???? I can't think of a single 
application in any language that would make use of your "linked list" 
... you lost the O(1) insertion time for example. I don't know about 
java LinkedList but I doubt it's like that !

Well, the fact your code is useless is not very important, but please, 
first, try to express what data structure you want and its properties. 
Then, write your tests, and don't forget important ones !!! (Like l > 4) 
Remember Python has no type checking, so you have to test with strange 
types too ! And follow the coding rules for python :

http://www.python.org/doc/essays/styleguide.html

Please, try again, and be carefull with your coding style when you post 
your code ;)

Pierre

Orri Ganel a écrit :
> I'm not at all sure this is, indeed, helpful *grin*.  I was just
> looking for a project to code, and decided to implement a LinkedList. 
> The how of using it is very simple: in the same way you would use a
> regular list.  Except for a few extra/different methods, the use of
> LinkedList is very similar to that of regular lists.  As to the why,
> like I said, I don't know that this will actually serve a purpose,
> it's a project I wrote just to write something.  If it actually ends
> up being useful, too, that'd be great, but it wasn't my main
> objective.
> 
> HTH,
> Orri
> 
> On Mon, 28 Mar 2005 15:50:15 -0800 (PST), Chad Crabtree
> <flaxeater at yahoo.com> wrote:
> 
>>I'm at a loss as to why this is helpful.  How and why would one use
>>this
>>instead of a regular list?  I know what linked lists are and why they
>>
>>would be useful in C++ or C or any other but not python.
> 
> 
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Tutor mailing list