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関数の2番目の引数では、この関数で文字列と整数を連結できないことに注意してください。
乾杯