title: Python recursion changing parameter ONLY inside if
body: <p><strong>[EDITED]</strong></p>
  
  <p>I'm having a really strange error and I can't quite wrap my head around it. How is it possible that id is still 1 (it's initialized to 1 when recursion starts) inside the for statment.</p>
  
  <pre><code>    def recursive_read(root, id, cursor, database):
          if root.text is not None:
              cursor.callproc('add_node', (id, root.text))
              database.commit()
              #print('Adding element to db... ')
              id = id+1
          for elem in root.getchildren():
              recursive_read(elem, id, cursor, database)
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This forms a perfect tree but unluckily it's not what I want. I want to "move" this indentation so child can reference correctly to parent, and for that I need id to increase only when element with text is encountered, so the problem remains.</p>
  
  <pre><code>def recursive_read(root, indent):
      if root.text is not None:
          print(' '*indent + '%s' % (root.text))
      for elem in root.getchildren():
          recursive_read(elem, indent+1)
  </code></pre>
  
  <p>What should I change so the id changes ONLY WHEN root.text is not None?</p>