Bare asterisk in function arguments?

后端 未结 6 1256
孤街浪徒
孤街浪徒 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:05

    While the original answer answers the question completely, just adding a bit of related information. The behaviour for the single asterisk derives from PEP-3102. Quoting the related section:

    The second syntactical change is to allow the argument name to
    be omitted for a varargs argument. The meaning of this is to
    allow for keyword-only arguments for functions that would not
    otherwise take a varargs argument:
    
        def compare(a, b, *, key=None):
            ...
    

    In simple english, it means that to pass the value for key, you will need to explicitly pass it as key="value".

提交回复
热议问题