Normal arguments vs. keyword arguments

后端 未结 10 905
一个人的身影
一个人的身影 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:55

    I'm surprised no one has mentioned the fact that you can mix positional and keyword arguments to do sneaky things like this using *args and **kwargs (from this site):

    def test_var_kwargs(farg, **kwargs):
        print "formal arg:", farg
        for key in kwargs:
            print "another keyword arg: %s: %s" % (key, kwargs[key])
    

    This allows you to use arbitrary keyword arguments that may have keys you don't want to define upfront.

提交回复
热议问题