如果在Python中为None,有没有简短的返回默认值?
在C#中,我可以说x ?? ""  x ?? "" ,如果x不为null,则会给我x,如果x为null,则会给出空string。 我发现它对于使用数据库很有用。 
如果Python在variables中findNone,是否有返回默认值的方法?
 你可以使用or运算符: 
 return x or "default" 
 请注意,如果x是任何其他的falsy值(包括空列表,0,空string,或者甚至是datetime.time(0) (午夜)),则也会返回"default" 。 
 return "default" if x is None else x 
尝试以上。
你可以使用一个条件expression式 :
 x if x is not None else some_value 
例:
 In [22]: x = None In [23]: print x if x is not None else "foo" foo In [24]: x = "bar" In [25]: print x if x is not None else "foo" bar 
 x or "default" 
效果最好 – 我甚至可以使用内联函数调用,而不执行两次或使用额外的variables:
 self.lineEdit_path.setText( self.getDir(basepath) or basepath ) 
 我用它打开Qt的dialog.getExistingDirectory()和取消,它返回空string。 
 你有三元语法x if x else '' – 那是你在做什么?