在python中,空字符串等於False,非空字符串等於真
>>> bool('')
False
>>> bool('foo')
True
布爾表達式的行爲描述爲in the python 2 documentation,對於python 3也是如此。
表達式x和y首先計算x;如果x爲假,則返回其值;否則,評估y並返回結果值。
表達式x或y首先計算x;如果x爲真,則返回其值;否則,評估y並返回結果值。
這就是爲什麼你得到字符串的 'https://' 或 'HTTP://' 根據 'self.use_ssl'
一些例子,從蟒蛇控制檯值:
>>> True or ''
True
>>> True or 'foo'
True
>>> False or ''
''
>>> False or 'foo'
'foo'
>>> '' or True
True
>>> '' or False
False
>>> 'bar' or True
'bar'
>>> 'bar' or False
'bar'
>>> True and ''
''
>>> True and 'foo'
'foo'
>>> False and ''
False
>>> False and 'foo'
False
>>> '' and True
''
>>> '' and False
''
>>> 'bar' and True
True
>>> 'bar' and False
False
您可以隨時使用布爾()
>>> 1 and 'bar'
'bar'
>>> bool(1 and 'bar')
True
如果self.use_ssl else'http://''imo –
同意@TimCastelijns,我將它讀作self.use_ssl和('https://' //'或'http://'),這是沒有意義的。 – SuperBiasedMan
'self.scheme ='https://'if kwargs.get('use_ssl',True)else'http://' – Ananth