如果其他蟒蛇函数调用或方法调用

问题描述:

,我们可以有,如果elif的其他基于呼叫的方法......例如:如果其他蟒蛇函数调用或方法调用

if line2[1] = '1': 
    a(line2) 
elif line2[1] = '2': 
    b(line2) 
elif line2[1] = '3': 
    c(line2) 

和这样的例子不胜枚举。 我们可以使用地图并调用函数。说的线路输入

实施例:

线= [ '1', '说', '您好']

线= [ '2', '如何', '是']

代码:

def g(line) 
    my_map = { '1': a(line), 
      '2': b(line), 
      '3': c(line, b), 
      ...... 
      and the list goes on 
      } 

here if line[0] = '1' call a(line) 
     elif line[0] = '2' call b(line) 

如何调用函数基础上输入。

请发送例子的代码,如果可能

由于 勒凯什


线= [ '1', '说', 'HI', '', '', '', '' ,'','',...继续5-15次] 在上面的例子中,如果我还必须指定其他变量。我该怎么做。

if line2[1] == '8': 
      p.plast = line2[3] 
      p.pfirst = line2[4] 
      p.pid = line2[9] 

elif line2[1] == 'I': 
      p.slast = line2[3] 
      p.sfirst = line2[4] 
      p.sid = line2[9] 
elif line2[1] == 'Q': 
      p.tlast = line2[3] 
      p.tfirst = line2[4] 
      p.tid = line2[9] 

这是否也有解决方法。

+0

而是加入一个包装函数'g',你可以有一个函数值字典。如果你需要传递一些默认参数,比如'3',你可以使用'lambda'。 –

+0

行被作为变量发送到这个函数。 g(行)从其他地方被调用。 。你能给我一个例子吗?我是新来的。 –

更新:一个棘手的方法

def make_change(line, p): 

    # Each value of the dict will be another dict in form property:index. 
    # Where property is key of p object to be set/updated, index is index of line with which property should be updated. 

    another_map = { 
     "V": {'vlast': 3, 'vfirst': 5, 'vnone': 7}, 
     "S": {'slast':2, 'sfirst':9, 'snone':4} 
    } 

    val = another_map.get(line[1], None) 
    if val is not None: 
     # iterating over val items to get which property to set and index if line to set 
     for key, item in val.iteritems(): 
      p[key] = line[item] 

    print p 


new_list = ["V", "S", 1,2,3,4,5,6,7,8,9,10,11,12,13] 
make_change(new_list, {}) 

现在改变new_list[1]末看到的输出


if line2[1] = '1': 
    a(line2) 
elif line2[1] = '2': 
    b(line2) 
elif line2[1] = '3': 
    c(line2) 

威尔完美的作品。

但是你的第二个方法是行不通的,因为当你写my_map = { '1': a(line), ... }my_map['1']None或返回函数的值(因为你调用的函数)

试试这个

def g(line): 
    # Note that value of dict is only the name of the function. 
    my_dict = { 
     '1': a, 
     '2': b 
     ... 
    } 

    # Assuming line[0] is 1,2,3, etc. Check if corresponding function exists in map_dict 
    # Convert line[0] to string before passing it in dict.get() method 
    call_func = my_dict.get(str(line[0]), None) 
    if call_func is not None: 
     call_func(line) 
+0

p是从其他地方调用的对象。 line2 = ['I','PO BOX 28082','VISION'] a_map = {'I':“id”,'S':“sn”,'4':“commercial”,} setattr(p,a_map [line2 [1]],line2 [2])我在执行这段代码时很有用。但是,如果line2 [0] =='I',我将如何分配:p.id = line2 [1] p.address = line2 [1]。 #不是地图的一部分p.status = line2 [2]#不是地图的一部分。正如你看到的,我不能在地图上分配它,因为它不是地图的一部分。如果可能,请用例子来解释。谢谢 - –

+0

对不起,我没明白'但是我将如何分配说,如果2号线[0] == '我':p.id = 2号线[1] p.address = 2号线[1]。 #不是地图的一部分p.status = line2 [2]#不是地图的一部分。正如你所看到的,我不能在地图上分配它,因为它不是地图的一部分!你可以用更多的细节来编辑你的问题吗? –

+0

我编辑了主要问题。如果我还不清楚,请告诉我。由于 –

你有正确的想法,但你有点错 - 你应该传递一个函数对象,而不是调用它。

def g(line): 
    my_map = {'1': a, '2': b, '3': c ... } 
    cur_func = my_map.get(line) 
    if cur_func is not None: 
     cur_func(line) 
    else: 
     #No mapping found .. 
+0

p是从其他地方调用的对象。 LINE2 = [ 'I', 'PO BOX 28082', 'VISION'] a_map = { 'I': “empolyer_id”, 'S': “SN”, '4': “商业” , } SETATTR(p,a_map [LINE2 [1]],LINE2 [2]) 我在执行此代码sucesfull。但是我怎么分配说 如果2号线[0] ==“我” –

+0

p是从别的地方调用的对象。 LINE2 = [ 'I', 'PO BOX 28082', 'VISION'] a_map = { 'I': “ID”, 'S': “SN”, '4': “商业” , } SETATTR(p,a_map [LINE2 [1]],LINE2 [2]) 我在执行此代码sucesfull。 但我将如何分配说 如果LINE2 [0] == 'I': p.id = LINE2 [1] p.address = LINE2 [1]。 #不是地图的一部分 p.status = line2 [2]#不是地图的一部分。正如你看到的,我不能在地图上分配它,因为它不是地图的一部分。如果可能,请用例子来解释。谢谢 –