如何在列表中定义对象属性

问题描述 投票:0回答:1

我对Python比较陌生。我正在尝试创建一个带有对象变量的可迭代列表,如下所示:

my_gps = MicropyGPS()
l1=[['Date and Time','',my_gps.date_string('long')],
    ['Latitude','',my_gps.latitude_string()],
     ..... etc
    ]

for i in range(0, (len(l1))):
    text = l1[i][2]     # I want 'text' to be set to the current value of 
                        # my_gps.date_string('long'), populated elsewhere in the code
    print (text)

`

我的想法是,我希望能够创建一个任意长的列表,我可以在其中放置所有属性,然后在运行时迭代该列表。但是,当实例化 l1 列表时,my_gps 属性的当前 values 将在 l1 列表中初始化,并且 my_gps 属性定义将被覆盖。

我已经搜索并尝试了我能想到的一切,例如将对象属性定义为字符串并尝试在运行时将其转换为正确的格式,以及使用 getattribute 和 setattribute 等函数,但一直无法使其工作。

我处理这个问题的方式是错误的吗?感谢任何见解。

python list object attributes
1个回答
0
投票
import micropygps
 my_gps = micropygps.MicropyGPS() 
l1 = [['Date and Time', '', ''], 
['Latitude', '', ''] ]
for i in range(0, len(l1)):
 l1[i][2] = my_gps.date_string('long') 
print(l1[i][2])
© www.soinside.com 2019 - 2024. All rights reserved.