[Tutor] Still on Linked Lists

Sheila King sheila@thinkspot.net
Wed, 04 Apr 2001 00:00:09 -0700


On Tue, 03 Apr 2001 18:46:22 -0600, VanL <vlindberg@verio.net>  wrote about
[Tutor] Still on Linked Lists:

:1.  I put together a new implementation of a linked list class:
:
:class LinkedList2:
:    
:    """ This class provides a generic node for linked lists.  Other
:python classes or
:    objects can inherit from this class to get the ability to represent
:themselves as
:    a linked list. """
:
:    def __init__(self, name, data=None, object=None):
:        self = (name, data, object)
:
:    def next(self):
:         return self[2]
: 
:    def label(self):
:        return self[0]
:
:    def setdata(self, object):
:        self[1] = object
:
:    def getdata(self):
:        return self[1]
:
:    def link(self, object):
:        self[2] = object
:
:    def unlink(self):
:        self[2] = None
:
:    def rename(self, name):
:        self[0] = name

It looked fine to me, so I pasted the above into an interpreter session, and
then tried your commands below:

:However, this doesn't work:
:
:>>> from LinkedList2 import *
:>>> this = LinkedList2('thisname')
:>>> that = LinkedList2('thatname', 'somedata', this)
:>>> theother = LinkedList2('theothername', 'someotherdata', that)
:>>> print theother
:
:Traceback (most recent call last):
:  File "<stdin>", line 1, in ?
:  File "LinkedList2.py", line 32, in __str__
:    print self[0], self[1], self[2]
:AttributeError: 'LinkedList2' instance has no attribute '__getitem__'
:>>>

I got no error. Here is my interpreter session:

>>> this = LinkedList2('thisname')
>>> that = LinkedList2('thatname', 'somedata', this)
>>> theother = LinkedList2('theothername', 'someotherdata', that)
>>> print theother
<__main__.LinkedList2 instance at 00B3601C>
>>> 

Mind you, since I pasted the code for your class definition directly into the
interpreter, I didn't need to use the "import" statement. I suspect this is
where you're having trouble.

So, I'll save your class as a file, called LinkedList2.py and try again, in a
new interpreter session.

Here it is:

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> from LinkedList2 import *
>>> this = LinkedList2('thisname')
>>> that = LinkedList2('thatname', 'somedata', this)
>>> theother = LinkedList2('theothername', 'someotherdata', that)
>>> print theother
<LinkedList2.LinkedList2 instance at 00B8FF3C>
>>> 

Hmm. Beats me why it doesn't work for you. Seems to work for me just fine ???

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/