How to access python objects with a dynamic object name?
Question: I have a question to one of my python scripts. I'm using the library untangle
(<https://github.com/stchris/untangle>) to import and convert xml config files
into the main script.

The problem: I have user informations in the config file for more than one
user and I'm using this information in a loop. It works very well, but it
makes the script very ugly due to the name of the generated objects from the
xml file.

Concrete this means I can't (or I just don't know how) change the name of the
object I would like to use dynamic.

The example code is below:

    
    
                if employee == 0:
                    if str(configobj.config.modes.employee.employee_1.name.cdata) != '':
                        display.drawtext(0,0,str(configobj.config.modes.employee.employee_1.name.cdata),"7x13B",255,255,255,True)
                        if str(configobj.config.modes.employee.employee_1.line1.cdata) != '':
                            display.drawtext(int(configobj.config.modes.employee.employee_1.line1['x']),
                                             int(configobj.config.modes.employee.employee_1.line1['y']),
                        if str(configobj.config.modes.employee.employee_1.line2.cdata) != '':
                            display.drawtext(int(configobj.config.modes.employee.employee_1.line2['x']),
                                             int(configobj.config.modes.employee.employee_1.line2['y']),
                        if str(configobj.config.modes.employee.employee_1.line3.cdata) != '':
                            display.drawtext(int(configobj.config.modes.employee.employee_1.line3['x']),
                                             int(configobj.config.modes.employee.employee_1.line3['y']))
                        displayimage = True
    
                elif employee == 1:
                    if str(configobj.config.modes.employee.employee_2.name.cdata) != '':
                        display.drawtext(0,0,str(configobj.config.modes.employee.employee_2.name.cdata),"7x13B",255,255,255,True)
                        if str(configobj.config.modes.employee.employee_2.line1.cdata) != '':
                            display.drawtext(int(configobj.config.modes.employee.employee_2.line1['x']),
                                             int(configobj.config.modes.employee.employee_2.line1['y']),
                        if str(configobj.config.modes.employee.employee_2.line2.cdata) != '':
                            display.drawtext(int(configobj.config.modes.employee.employee_2.line2['x']),
                                             int(configobj.config.modes.employee.employee_2.line2['y']),
                        if str(configobj.config.modes.employee.employee_2.line3.cdata) != '':
                            display.drawtext(int(configobj.config.modes.employee.employee_2.line3['x']),
                                             int(configobj.config.modes.employee.employee_2.line3['y']),
                        if str(configobj.config.modes.employee.employee_2.image.cdata) != '':
                            display.showimage(160,0,str(configobj.config.modes.employee.employee_2.image.cdata))
                        displayimage = True
    

And this is a lot of repeated code with a changing number. How can I improve
this?


Answer: Use [getattr](https://docs.python.org/3.5/library/functions.html#getattr):

    
    
    getattr(configobj.config.modes.employee, 'employee_' + str(employee + 1)).name.cdata
    

You can also create separate variable for employee:

    
    
    employee = getattr(configobj.config.modes.employee, 'employee_' + str(employee + 1))
    print(employee.name.cdata)
    print(employee.line1['x'])
    

