Python arguments as a dictionary

前端 未结 2 1224
囚心锁ツ
囚心锁ツ 2020-12-28 12:11

How can I get argument names and their values passed to a method as a dictionary?

I want to specify the optional and required parameters for a GET request as part of

相关标签:
2条回答
  • 2020-12-28 12:46

    For non-keyworded arguments, use a single *, and for keyworded arguments, use a **.

    For example:

    def test(*args, **kwargs):
        print args
        print kwargs
    
    >>test(1, 2, a=3, b=4)
    (1, 2)
    {'a': 3, 'b': 4}
    

    Non-keyworded arguments would be unpacked to a tuple and keyworded arguments would be unpacked to a dictionary. Unpacking Argument Lists

    0 讨论(0)
  • 2020-12-28 12:48

    Use a single argument prefixed with **.

    >>> def foo(**args):
    ...     print(args)
    ...
    >>> foo(a=1, b=2)
    {'a': 1, 'b': 2}
    
    0 讨论(0)
提交回复
热议问题