我试图将**kwargs
传递给另一个函数,但仅在其不为空时传递。目前我有这个if else
,我想知道是否有更有效、更符合Python风格的方法?
if other:
html.append(self.render_option(val, label, selected, **other))
else:
html.append(self.render_option(val, label, selected))
如果 other 是
NoneType
,我会收到错误提示:...argument after ** must be a mapping, not NoneType
if other is not None
而不是if other
),但这只是为了语法上的优雅,肯定不会影响执行。 - Adam Smithargument after ** must be a mapping, not NoneType
。 - Johnston