Python normal arguments vs. keyword arguments?

Python normal arguments vs. keyword arguments?

Asked on November 13, 2018 in Python.
Add Comment


  • 3 Answer(s)

        Except the order doesn’t matter in keyword the keyword arguments is same  as normal arguments.

        For example: the two functions calls below are the same.

    def foo(bar, baz):
        pass
    foo(1, 2)
    foo(baz=2, bar=1)
    
    Answered on November 13, 2018.
    Add Comment

       It is easy to 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
    >>>
    
    Answered on November 13, 2018.
    Add Comment

        In this ,mix  the positional and keyword arguments to do sneaky things like this using *args and **kwargs.

    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 keyswhich is not define in upfront.

    Answered on November 13, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.