最佳答案
                                        
                                                                        
                                我有这样一个类:
class User:
def __init__(self):
self.data = []
self.other_data = []
def doSomething(self, source):
// if source = 'other_data' how to access self.other_data
我想为doSomething中的源变量传递一个字符串,并访问同名的类成员。
我已经尝试了getattr,它只适用于函数(从我可以告诉),以及有User扩展dict和使用self.__getitem__,但这也不起作用。最好的方法是什么?
 
                                
                             
                                
                             
                                
                            