当前位置: 代码迷 >> python >> 在Python中使用切换器时传递多个参数
  详细解决方案

在Python中使用切换器时传递多个参数

热度:16   发布时间:2023-07-14 08:43:19.0
device_properties = ['Device Category', 'Device Count', 'Device Cost']

switcher = {
                'Device Category': get_device_category(index),
                'Device Count': get_device_count(index),
                'Device Cost': get_device_cost(index)
            }        

for index in range(10):
     for j in device_properties:
         cat_data[cat] = switcher.get(j, 0)

现在,我想在调用switcher.get(j, 0)时也将“ index”作为参数发送。 switcher.get((j,index), 0)以便我可以在switcher dict中定义的函数中使用index的值。 那么,有没有办法在switcher.get(j, 0)或任何替代方法时发送多个参数?

您可以存储指向函数的指针,并在访问时调用它们:

switcher = {
    'Device Category': get_device_category,
    'Device Count': get_device_count,
    'Device Cost': get_device_cost,
}        

for index in range(10):
     for j in device_properties:
         cat_data[cat] = switcher.get(j)(index)