我们可以使用
如果我通过
** kw
将字典转换为kw
,但是如果我想要kw
作为str(kw)
而不是str(dict)
,因为我想要一个带有关键字参数的字符串给code_generator
,如果我通过
obj.method(name='name', test='test', relation = [('id','=',1)])
I want a function to return the string like
"name='name', test='test', relation = [('id','=',1)]"
.iteritems()
是Python 2的语法;在Python 3中应使用.items()
。参见https://dev59.com/QWkv5IYBdhLWcg3wpCQz#10458567 - Nathan Wailes", ".join(f'{x[0]}={x[1]!r}' for x in kwargs.items())
- James Owers", ".join(f"{key}={value}" for key, value in kwargs.items())
或许更符合 Python 风格。 - cbrnr