2014-03-12 149 views
0

我有下面的代码,我希望在运行时感受函数映射的参数。它应该像Python中函数的参数

#devices = map(InputDevice, ('/dev/input/event15','/dev/input/event16')) 

但是,当我尝试在运行时执行它,它不起作用。这里是我的尝试:

readers = "" 
devices = map(InputDevice, list_devices()) 
for dev in devices: 
    if "深" in dev.name or "Barcode" in dev.name: 
     if readers == "": 
     readers = "'" + dev.fn + "'" 
     else: 
     readers = readers + ", '" + dev.fn + "'" 

devices = map(InputDevice, (readers)) 

,读者准确显示'的/ dev /输入/ event15', '的/ dev /输入/ event16',但此字符串不为参数工作。我猜这是因为逗号不起作用。有谁知道我该怎么做?

此功能是evdev的一部分。

从此开始感谢! 此致敬礼, Erik

回答

1

它看起来像我想你想readers是一个非字符串迭代。也许尝试:

devices = map(InputDevice, readers.split(',')) 

这将分裂readers到一个列表,而不是保持它作为一个字符串。

这仍然不是特别干净的代码。更好的做法是先建立一个列表:

readers = [] 
devices = map(InputDevice, list_devices()) 
for dev in devices: 
    if "深" in dev.name or "Barcode" in dev.name: 
     readers.append(dev.fn) 

devices = map(InputDevice, (readers)) 
+0

非常感谢。它完美的作品。这正是我需要的! –