【问题标题】:Python __dict__ conversion to kwargs [duplicate]Python __dict__ 转换为 kwargs [重复]
【发布时间】:2018-01-08 06:39:22
【问题描述】:

我想使用类继承为 sunburnt(solr 接口)构建一个查询,因此将键值对添加在一起。 sunburnt 接口采用关键字参数。如何将 dict ({'type':'Event'}) 转换为关键字参数 (type='Event')?

【问题讨论】:

    标签: python


    【解决方案1】:

    您可以使用字典解包来做到这一点:

    dct = dict({'type':'Event'})
    
    # equivalent to func(type='Event')
    func(**dct)
    

    【讨论】:

    • dict() 的意义何在? {'type':'Event'} 已经是一个字典了。
    • 为了他而复制了 OPs 代码。
    • @SiyuanRen:原来的问题中。也许问他们? (即,通过stackoverflow.com/questions/5710391/… 向 teaforthecat 提问)
    猜你喜欢
    • 2010-10-24
    • 2011-08-08
    • 2014-06-25
    • 1970-01-01
    • 2012-04-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多