title: Using self.xxxx as a default parameter - Python
body: <p>I'm trying to simplify one of my homework problems and make the code a little better. What I'm working with is a binary search tree. Right now I have a function in my <code>Tree()</code> class that finds all the elements and puts them into a list.</p>
  
  <pre><code>tree = Tree()
  #insert a bunch of items into tree
  </code></pre>
  
  <p>then I use my makeList() function to take all the nodes from the tree and puts them in a list.
  To call the <code>makeList()</code> function, I do <code>tree.makeList(tree.root)</code>. To me this seems a little repetitive. I'm already calling the tree object with <code>tree.</code>so the <code>tree.root</code> is just a waste of a little typing.</p>
  
  <p>Right now the makeList function is:</p>
  
  <pre><code>    def makeList(self, aNode):
          if aNode is None:
              return []
          return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)
  </code></pre>
  
  <p>I would like to make the aNode input a default parameter such as <code>aNode = self.root</code> (which does not work) that way I could run the function with this, <code>tree.makeList()</code>. </p>
  
  <p>First question is, why doesn't that work?<br>
  Second question is, is there a way that it can work? As you can see the <code>makeList()</code> function is recursive so I cannot define anything at the beginning of the function or I get an infinite loop.</p>
  
  <p><strong>EDIT</strong>
  Here is all the code as requested:</p>
  
  <pre><code>class Node(object):
      def __init__(self, data):
          self.data = data
          self.lChild = None
          self.rChild = None
  
  class Tree(object):
      def __init__(self):
          self.root = None
  
      def __str__(self):
          current = self.root
  
      def isEmpty(self):
          if self.root == None:
              return True
          else:
              return False
  
      def insert (self, item):
          newNode = Node (item)
          current = self.root
          parent = self.root
  
          if self.root == None:
              self.root = newNode
          else:
              while current != None:
                  parent = current
                  if item &lt; current.data:
                      current = current.lChild
                  else:
                      current = current.rChild
  
              if item &lt; parent.data:
                  parent.lChild = newNode
              else:
                  parent.rChild = newNode
  
      def inOrder(self, aNode):
          if aNode != None:
              self.inOrder(aNode.lChild)
              print aNode.data
              self.inOrder(aNode.rChild)
  
      def makeList(self, aNode):
          if aNode is None:
              return []
          return [aNode.data] + self.makeList(aNode.lChild) + self.makeList(aNode.rChild)
  
  
      def isSimilar(self, n, m):
          nList = self.makeList(n.root)
          mList = self.makeList(m.root) 
          print mList == nList 
  </code></pre>