0

I am trying to find out if there is a way to convert named arguments to dict.

I understand using **kwargs in place of individual named arguments would be pretty straight forward.

def func(arg1=None, arg2=None, arg3=None):
    # How can I convert these arguments to {'arg1': None, 'arg2': None, 'arg3': None}`
Darius
  • 10,762
  • 2
  • 29
  • 50
user6037143
  • 516
  • 5
  • 20
  • Could you please show what you indend to do with that? – Mad Physicist Feb 12 '19 at 21:24
  • Because the answer is just `{'arg1': None, 'arg2': None, 'arg3': None}` – Mad Physicist Feb 12 '19 at 21:26
  • I can't imagine a use case for this. Would you be able to post the use-case as well explaining why you need this kind of structure? – mad_ Feb 12 '19 at 21:29
  • `func(..., **kwargs)` is for when you're converting named args on the **calling**-side to a dict on the **function-definition** side. But you want to convert one into the other, **both on the function-definition side**. That would be weird. Please explain why you want to do this (i.e. also show the calling-side syntax, and state what actual problem you're trying to solve). – smci Feb 12 '19 at 21:29
  • Possible duplicate of [Getting list of parameter names inside python function](https://stackoverflow.com/questions/582056/getting-list-of-parameter-names-inside-python-function) – Iluvatar Feb 12 '19 at 21:39

1 Answers1

5

You can use locals() to get the local arguments:

def func(arg1=None, arg2=None, arg3=None):
    print(locals())

func()  # {'arg3': None, 'arg2': None, 'arg1': None}
Darius
  • 10,762
  • 2
  • 29
  • 50