chainsol

Untitled

Jul 11th, 2017
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. class ObjectA(object):
  2.     '''Might have a parent and might have an array of children'''
  3.     def __init__(self, parent=None):
  4.         self.children = []
  5.         if parent:
  6.             parent.add_child(self)
  7.         self.parent = parent
  8.  
  9.     def is_parent(self):
  10.         if self.children:
  11.             return True
  12.         else:
  13.             return False
  14.  
  15.     def add_child(self, child):
  16.         self.children.append(child)
  17.         child.parent = self
  18.  
  19.     def get_parent_array(self):
  20.         if self.parent:
  21.             return self.parent.children
  22.         else:
  23.             return None
  24.  
  25.  
  26. if __name__ == "__main__":
  27.     p = ObjectA()
  28.     c1 = ObjectA(parent=p)
  29.     c2 = ObjectA(parent=p)
  30.     c3 = ObjectA(parent=p)
  31.  
  32.     print(p.children)
Advertisement
Add Comment
Please, Sign In to add comment