在这种情况下
>>> x = {}
>>> x.get('test') #Prints None
>>> x.get('test','') #Prints empty string
''
>>> x = {'test':None}
>>> x.get('test') #Prints None
>>> x.get('test','') #Prints None
如何通过将无值键视为不存在而在两种情况下都打印空字符串?
答案 0 :(得分:3)
将None
调用包含在检查值是否为def get_except_None(d, key):
val = d.get(key, '')
if val is None:
return ''
return val
x.get('test', '')
您的代码中不是get_except_None(x, test)
,而是baumWelch