mercredi 25 janvier 2017

Python recursion changing parameter ONLY inside if

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.

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)

What should I change so the id changes ONLY WHEN root.text is not None?

Aucun commentaire:

Enregistrer un commentaire