Bare asterisk in function arguments?

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

    def func(*, a, b):
        print(a)
        print(b)
    
    func("gg") # TypeError: func() takes 0 positional arguments but 1 was given
    func(a="gg") # TypeError: func() missing 1 required keyword-only argument: 'b'
    func(a="aa", b="bb", c="cc") # TypeError: func() got an unexpected keyword argument 'c'
    func(a="aa", b="bb", "cc") # SyntaxError: positional argument follows keyword argument
    func(a="aa", b="bb") # aa, bb
    

    the above example with **kwargs

    def func(*, a, b, **kwargs):
        print(a)
        print(b)
        print(kwargs)
    
    func(a="aa",b="bb", c="cc") # aa, bb, {'c': 'cc'}
    

提交回复
热议问题