Bare asterisk in function arguments?

后端 未结 6 1278
孤街浪徒
孤街浪徒 2020-11-22 06:22

What does a bare asterisk in the arguments of a function do?

When I looked at the pickle module, I see this:

pickle.dump(obj, file, protocol=None, *,         


        
6条回答
  •  眼角桃花
    2020-11-22 07:19

    Suppose you have function:

    def sum(a,key=5):
        return a + key 
    

    You can call this function in 2 ways:

    sum(1,2) or sum(1,key=2)

    Suppose you want function sum to be called only using keyword arguments.

    You add * to the function parameter list to mark the end of positional arguments.

    So function defined as:

    def sum(a,*,key=5):
        return a + key 
    

    may be called only using sum(1,key=2)

提交回复
热议问题