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

后端 未结 6 978
长发绾君心
长发绾君心 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:51

    Another way to go about it, besides the nice answers already mentioned, depends upon the fact that you can pass optional named arguments by position. For example,

    def f(x,y=None):
        print(x)
        if y is not None:
            print(y)
    

    Yields

    In [11]: f(1,2)
    1
    2
    
    In [12]: f(1)
    1
    

提交回复
热议问题