【发布时间】:2021-11-03 07:47:41
【问题描述】:
我有一个类似如下的课程:
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__,但这也不起作用。最好的方法是什么?
【问题讨论】: