我有这样的功能:
def __init__(self, out_file='out.csv', tmp_folder=None):
self.tmp_folder = tmp_folder if tmp_folder != None else join(getcwd(), '__tmp__')
我想知道是否存在更聪明的方法,比如js:
self.tmp_folder = tmp_folder || join(getcwd(), '__tmp__')
答案 0 :(得分:2)
由于None
评估为False
,您可以随时执行:
def __init__(self, out_file='out.csv', tmp_folder=None):
self.tmp_folder = tmp_folder or join(getcwd(), '__tmp__')
但请注意,如果self.tmp_folder
为任何虚假值(join(getcwd(), '__tmp__')
,tmp_folder
,False
,{0
,则[]
会将{}
分配给{{1}} {1}}等。)
答案 1 :(得分:2)
是的,就像这样:
self.tmp_folder = tmp_folder or join(getcwd(), '__tmp__')
您只需使用or
代替||