Normal arguments vs. keyword arguments

后端 未结 10 755
一个人的身影
一个人的身影 2020-11-22 02:35

How are \"keyword arguments\" different from regular arguments? Can\'t all arguments be passed as name=value instead of using positional syntax?

10条回答
  •  無奈伤痛
    2020-11-22 02:52

    Positional Arguments

    They have no keywords before them. The order is important!

    func(1,2,3, "foo")
    

    Keyword Arguments

    They have keywords in the front. They can be in any order!

    func(foo="bar", baz=5, hello=123)
    
    func(baz=5, foo="bar", hello=123)
    

    You should also know that if you use default arguments and neglect to insert the keywords, then the order will then matter!

    def func(foo=1, baz=2, hello=3): ...
    func("bar", 5, 123)
    

提交回复
热议问题