python convert a string to arguments list

后端 未结 3 890
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 12:39

Can I convert a string to arguments list in python?

def func(**args):
    for a in args:
        print a, args[a]

func(a=2, b=3)

# I want the following work li         


        
3条回答
  •  没有蜡笔的小新
    2021-02-08 13:14

    You could massage the input string into a dictionary and then call your function with that, e.g.

    >>> x='a=2, b=3'
    >>> args = dict(e.split('=') for e in x.split(', '))
    >>> f(**args)
    a 2
    b 3
    

提交回复
热议问题