A question about reference in Python.

James Mills prologic at shortcircuit.net.au
Mon Dec 8 07:17:57 CET 2008


On Mon, Dec 8, 2008 at 4:13 PM, Chris Rebert <clp at rebertia.com> wrote:
> The following three lines serve no purpose and can only lead to confusion:
>>   value = None
>>   prev = None
>>   next = None

You are absolutely right :)

Updated code:

#!/home/jmills/bin/python -i

class Node(object):

   def __init__(self, value, prev=None, next=None):
      self.value = value
      self.prev = prev
      self.next = next

class List(object):

   def __init__(self, *seq):
      if seq:
         first = prev = node = None
         for x in seq:
            if not first:
               first = Node(x)
               prev = node = first
            else:
               node = Node(x, prev)
               prev.next = node
               prev = node

         self.data = first
      else:
         self.data = None

   def __getitem__(self, x):
      node = self.data
      for i in xrange(x):
         node = node.next
      return node.value

x = List(0, 1, 2, 3)

cheers
Jaems

PS: Sorry Chris :)

-- 
--
-- "Problems are solved by method"



More information about the Python-list mailing list