I have a simple class that stores attributes:

In [1]: class Test(object):
   ...:     a=10
   ...:     b=20

I have a function that returns attributes using attrgetter, and it accepts these through the args parameter to allow a user to pass various attribute fieldnames in. For example:

In [4]: def getstuff(*args):
   ...:     return attrgetter(args)

I can manually pass multiple arguments to an attrgetter and return the attributes from my class:

In [10]: f=attrgetter('a','b')

In [11]: f(test)
Out[11]: (10, 20)

However, my getstuff function doesn't work because args is a list and attrgetter takes only comma separated strings:

n [18]: f=getstuff('a','b')

In [19]: f(test)
TypeError                                 Traceback (most recent call last)
<ipython-input-19-efe923d88401> in <module>()
----> 1 f(test)

TypeError: attribute name must be a string

Aka, the problem is args is being converted into a list. How do I get a list, say ['a', 'b'] into the attrgetter? attrgetter(['a','b']) ---> attrgetter('a','b')


This question has already been answered. Start a new discussion instead.