执行以下操作最惯用的方法是什么?
def xstr(s):
if s is None:
return ''
else:
return s
s = xstr(a) + xstr(b)
更新:我正在采纳 Trypich 的使用 str(s) 的建议,这使得该例程适用于字符串以外的其他类型。Vinay Sajip 的 lambda 建议给我留下了深刻的印象,但我想让我的代码保持相对简单。
def xstr(s):
if s is None:
return ''
else:
return str(s)