[Tutor] Doubly linked list getting "AttributeError: 'NoneType' object has no attribute 'element'"

Steven D'Aprano steve at pearwood.info
Mon Sep 21 20:13:01 CEST 2015


Hi Sudhier, and welcome,

Unfortunately I'm about to step away from the computer, so I can't 
really answer your question in detail, but since it's been 2 or 3 days 
since you asked your question, and I haven't seen any replies at all, I 
thought I'd give a quick reply so you know we aren't ignoring you.

(My inbox is suffering from overload at the moment, so it is possible 
that others have answered your question and I haven't noticed. If so, my 
apologies for wasting everyone's time.)

Regards,


Steve

On Sat, Sep 19, 2015 at 07:52:07PM +0530, Sudhier Batara wrote:
> Hi,
> 
> I have the below code which is a doubly linked list , which returns
> the Position of the node rather than the node itself.The code is quite
> self explanatory.In the below code i call following methods ,
> 
> p = L.add_last(8)
> print p.element()
> 
> the above methods works fine adds the element into the List instance
> and also printing the element shows the element in the list.
> 
> But when i call the below methods,
> 
> q = L.add_after(p,5)
> q.element()
> 
> throws following error
> 
> 
> _______________________________________
> in make position
> in make position
> 8
> inside validate
> 8
> in make position
> Traceback (most recent call last):
>   File "positional_dlist.py", line 128, in <module>
>     print q.element()
>   File "positional_dlist.py", line 45, in element
>     return self._node.element
> AttributeError: 'NoneType' object has no attribute 'element'
> ____________________________________________________
> 
> Complete code below
> 
> 
> 
> #!/usr/bin/python
> 
> class _DoublelinkedBase:
>     class _Node:
>         def __init__(self,element,prev,Next):
>             self.element = element
>             self._prev = prev
>             self._Next = Next
> 
>     def __init__(self):
>         self._header = self._Node(None,None,None)
>         self._trailer = self._Node(None,None,None)
>         self._header._Next = self._trailer
>         self._trailer._prev = self._header
>         self._size = 0
> 
>     def __len__(self):
>         return self._size
> 
>     def is_empty(self):
>         return self._size == 0
> 
>     def _insert_between(self,element,predecessor,successor):
>         newest = self._Node(element,predecessor,successor)
>         predecessor._Next = newest
>         successor._prev = newest
>         self._size += 1
> 
>     def _delete_node(self,node):
>         predecessor = node._prev
>         successor = node._Next
>         predecessor._Next = successor
>         successor._prev = predecessor
>         element = node.element
>         node.element = node._Next = node._prev = None
>         self._size -= 1
> 
> class PositionalList(_DoublelinkedBase):
>     class Position:
>         def __init__(self,container,node):
>             self._container = container
>             self._node = node
> 
>         def element(self):
>             return self._node.element
> 
>         def __eq__(self,other):
>             return type(other) is type(self) and other._node is self._node
> 
>         def __ne__(self,other):
>             return  not (other == self)
> 
>     def _validate(self, p):
>         if not isinstance(p,self.Position):
>             raise TypeError("p is not of type Position")
>         if p._container is not self:
>             raise ValueError("p is not the instance of this list")
>         if p._node._Next is None:
>             print p._node
>             raise ValueError("No longer a valid node")
>         print "inside validate"
>         print p.element()
>         return p._node
> 
>     def _make_position(self,node):
>         if node is self._header or node is self._trailer:
>             print "in make position for none"
>             return None
>         else:
>             print "in make position"
>             return self.Position(self,node)
> 
>     def first(self):
>         return self._make_position(self._header._Next)
> 
>     def last(self):
>         return self._make_position(self._trailer._prev)
> 
>     def before(self,p):
>         node = self._validate(p)
>         return self._make_position(node._prev)
> 
>     def after(self,p):
>         node = self._validate(p)
>         return self._make_position(node._Next)
> 
>     def __iter__(self):
>        cursor = self.first()
>        while cursor is not None:
>            yield  cursor.element()
>            cursor = self.after(cursor)
> 
>     def _insert_between(self,e,predecessor,successor):
>         #node =
> super(_DoublelinkedBase,self)._insert_between(e,predecessor,successor)
>         node = _DoublelinkedBase()._insert_between(e,predecessor,successor)
>         return self._make_position(node)
> 
>     def add_first(self,e):
>         return self._insert_between(e,self._header,self._header._Next)
> 
>     def add_last(self,e):
>         return self._insert_between(e,self._trailer._prev,self._trailer)
> 
>     def add_before(self,p,e):
>         original = self._validate(p)
>         return self._insert_between(e,original._prev,original)
> 
>     def add_after(self,p,e):
>         original = self._validate(p)
>         return self._insert_between(e,original,original._Next)
> 
>     def delete_node(self,p):
>         original = self._validate(p)
>         return self._delete_node(original)
> 
>     def replace(self,p,e):
>         original = self._validate(p)
>         old_value = original.element
>         original.element = e
>         return old_value
> 
> 
> L = PositionalList()
> p = L.add_last(8)
> p = L.first()
> print p.element()
> q = L.add_after(p,5)
> print q.element()
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list