gpt4 book ai didi

python - 初始化列表在方法中显示为空

转载 作者:太空宇宙 更新时间:2023-11-04 06:05:07 25 4
gpt4 key购买 nike

这是我的代码:

serial_list=[]
dev_label = ["0","0"]
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='block',device_type='partition')
observer = GUDevMonitorObserver(monitor)

print dev_label


def device_connected(observer, device):
print dev_label

flag =False
for iden in serial_list :
if iden == device.__getitem__('ID_SERIAL_SHORT'):
flag=True

if flag ==False:

Welcome.device_count+=1
print Welcome.device_count
serial_list.append(device.__getitem__('ID_SERIAL_SHORT'))
dev_label[Welcome.device_count-1]=str(device.__getitem__('ID_FS_LABEL'))
label = gtk.Label('Device connected :: {0!r}'.format(dev_label[Welcome.device_count-1]))
Welcome.vbox.pack_start(label)
Welcome.window.show_all()

if Welcome.device_count<2:
label = gtk.Label('Connect the second device')
Welcome.vbox.pack_start(label)
Welcome.window.show_all()


else :
Exchange()

observer.connect("device-added",device_connected)
monitor.start()

class Welcome:
device_count = 0
window = gtk.Window()
vbox= gtk.VBox(False, 5)


def __init__(self):

self.window.set_default_size(300, 300)
self.window.set_title("Welcome")

label = gtk.Label("Connect the desired device")

self.vbox.pack_start(label)
self.window.add(self.vbox)

self.window.connect("destroy", lambda q: gtk.main_quit())
self.window.show_all()

这是回溯:

['0', '0']
[]
1
Traceback (most recent call last):
File "project.py", line 36, in device_connected
dev_label[Welcome.device_count-1]=str(device.__getitem__('ID_FS_LABEL'))
IndexError: list assignment index out of range

如您所见,dev_label 列表在开始时被初始化为 ["0","0"]。但是,此列表在 device_connected 中显示为空。有人请告诉我出了什么问题!

最佳答案

Welcome.device_count 的值为 0,因此您分配的值为

array_name[0 - 1] = str(...);

因此,Welcome.device_count 是导致错误的原因。

谢谢!

关于python - 初始化列表在方法中显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22579321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com