Python multilevel getattr with string

Instead of using getattr you should use attrgetter which allows you to specify using the dot notation. This does require a minor change to your function as shown below.

from operator import attrgetter

def analyze_data(myvar, dataname):
    fn = attrgetter(dataname)
    data = fn(myvar)
    # Do something with data, e.g.:
    print(str(data[0]))

analyze_data(instance1, 'data1')
1

analyze_data(instance1, 'subdata.data2')
4

You can recurse into the data by splitting the dotted path and following it:

def analyze_data(myvar, dataname):
    for dn in dataname.split('.'):
        myvar = getattr(myvar, dn)
    # Do something with data, e.g.:
    print(str(myvar[0]))

This keeps updating myvar until you are at the end of the path.


functools.reduce is another option.

>>> from functools import reduce
>>> reduce(getattr, 'subdata.data2'.split('.'), instance1)
[4, 5, 6]

One way to achieve what you want without changing the function at all is this:

class a:
pass

instance1 = a()

# Create 1st data set
instance1.data1 = [1,2,3]

# Create 2nd data set
instance1.subdata = a()
instance1.subdata.data2 = [4,5,6]


def analyze_data(myvar, dataname):
    data = getattr(myvar, dataname)
    # Do something with data, e.g.:
    print(str(data[0]))

analyze_data(instance1, 'data1')
analyze_data(instance1.subdata, 'data2')