在Python中使用getattr/setattr访问列表项

9 投票
3 回答
22500 浏览
提问于 2025-04-16 22:38

我在尝试用Python中的getattr和setattr函数来访问或赋值列表里的项目。可惜的是,似乎没有办法把列表的名称和索引位置一起传递进去。
以下是我一些尝试的示例代码:

class Lists (object):
  def __init__(self):
    self.thelist = [0,0,0]

Ls = Lists()

# trying this only gives 't' as the second argument.  Python error results.
# Interesting that you can slice a string to in the getattr/setattr functions
# Here one could access 'thelist' with with [0:7]
print getattr(Ls, 'thelist'[0])


# tried these two as well to no avail.  
# No error message ensues but the list isn't altered. 
# Instead a new variable is created Ls.'' - printed them out to show they now exist.
setattr(Lists, 'thelist[0]', 3)
setattr(Lists, 'thelist\[0\]', 3)
print Ls.thelist
print getattr(Ls, 'thelist[0]')
print getattr(Ls, 'thelist\[0\]')

另外要注意,在attr函数的第二个参数中,你不能把字符串和整数直接拼接在一起。

谢谢!

3 个回答

3

正如你发现的那样,__getattr__ 不是这样工作的。如果你真的想使用列表索引的话,可以使用 __getitem____setitem__,而不需要考虑 getattr()setattr()。可以这样做:

class Lists (object):

    def __init__(self):
        self.thelist = [0,0,0]

    def __getitem__(self, index):
        return self.thelist[index]

    def __setitem__(self, index, value):
        self.thelist[index] = value

    def __repr__(self):
        return repr(self.thelist)

Ls = Lists()
print Ls
print Ls[1]
Ls[2] = 9
print Ls
print Ls[2]
5

你可以这样做:

l = getattr(Ls, 'thelist')
l[0] = 2  # for example
l.append("bar")
l is getattr(Ls, 'thelist')  # True
# so, no need to setattr, Ls.thelist is l and will thus be changed by ops on l

getattr(Ls, 'thelist') 会给你一个指向同一个列表的引用,这个列表也可以通过 Ls.thelist 来访问。

9
getattr(Ls, 'thelist')[0] = 2
getattr(Ls, 'thelist').append(3)
print getattr(Ls, 'thelist')[0]

如果你想像这样使用 getattr(Ls, 'thelist[0]'),你需要重写一下 __getattr__ 这个方法,或者使用内置的 eval 函数。

撰写回答