Can a variable number of arguments be passed to a function?

后端 未结 6 783
长发绾君心
长发绾君心 2020-11-22 00:32

In a similar way to using varargs in C or C++:

fn(a, b)
fn(a, b, c, d, ...)
6条回答
  •  抹茶落季
    2020-11-22 00:43

    def f(dic):
        if 'a' in dic:
            print dic['a'],
            pass
        else: print 'None',
    
        if 'b' in dic:
            print dic['b'],
            pass
        else: print 'None',
    
        if 'c' in dic:
            print dic['c'],
            pass
        else: print 'None',
        print
        pass
    f({})
    f({'a':20,
       'c':30})
    f({'a':20,
       'c':30,
       'b':'red'})
    ____________
    

    the above code will output

    None None None
    20 None 30
    20 red 30
    

    This is as good as passing variable arguments by means of a dictionary

提交回复
热议问题