Normal arguments vs. keyword arguments

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

    Just suplement/add a way for defining the default value of arguments that is not assigned in key words when calling the function:

    def func(**keywargs):
    if 'my_word' not in keywargs:
        word = 'default_msg'
    else:
        word = keywargs['my_word']
    return word
    

    call this by:

    print(func())
    print(func(my_word='love'))
    

    you'll get:

    default_msg
    love
    

    read more about *args and **kwargs in python: https://www.digitalocean.com/community/tutorials/how-to-use-args-and-kwargs-in-python-3

    0 讨论(0)
  • 2020-11-22 02:45

    I'm surprised that no one seems to have pointed out that one can pass a dictionary of keyed argument parameters, that satisfy the formal parameters, like so.

    >>> def func(a='a', b='b', c='c', **kwargs):
    ...    print 'a:%s, b:%s, c:%s' % (a, b, c)
    ... 
    >>> func()
    a:a, b:b, c:c
    >>> func(**{'a' : 'z', 'b':'q', 'c':'v'})
    a:z, b:q, c:v
    >>> 
    
    0 讨论(0)
  • 2020-11-22 02:47

    There are two related concepts, both called "keyword arguments".

    On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which were not mentioned at all.

    The other concept is on the function definition side: you can define a function that takes parameters by name -- and you don't even have to specify what those names are. These are pure keyword arguments, and can't be passed positionally. The syntax is

    def my_function(arg1, arg2, **kwargs)
    

    Any keyword arguments you pass into this function will be placed into a dictionary named kwargs. You can examine the keys of this dictionary at run-time, like this:

    def my_function(**kwargs):
        print str(kwargs)
    
    my_function(a=12, b="abc")
    
    {'a': 12, 'b': 'abc'}
    
    0 讨论(0)
  • 2020-11-22 02:47

    There are two ways to assign argument values to function parameters, both are used.

    1. By Position. Positional arguments do not have keywords and are assigned first.

    2. By Keyword. Keyword arguments have keywords and are assigned second, after positional arguments.

    Note that you have the option to use positional arguments.

    If you don't use positional arguments, then -- yes -- everything you wrote turns out to be a keyword argument.

    When you call a function you make a decision to use position or keyword or a mixture. You can choose to do all keywords if you want. Some of us do not make this choice and use positional arguments.

    0 讨论(0)
  • 2020-11-22 02:49

    I was looking for an example that had default kwargs using type annotation:

    def test_var_kwarg(a: str, b: str='B', c: str='', **kwargs) -> str:
         return ' '.join([a, b, c, str(kwargs)])
    

    example:

    >>> print(test_var_kwarg('A', c='okay'))
    A B okay {}
    >>> d = {'f': 'F', 'g': 'G'}
    >>> print(test_var_kwarg('a', c='c', b='b', **d))
    a b c {'f': 'F', 'g': 'G'}
    >>> print(test_var_kwarg('a', 'b', 'c'))
    a b c {}
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题