今天看到別人使用 2個變數做 or 運算,覺得滿神奇的。
手動測試一下這一個寫法,
a=None b="123" c=2
a or b = ‘123’
a or c = 2
b or c = ‘123’
資料來源:
https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/extractor/peertube.py
Python: most idiomatic way to convert None to empty string?
Because None is False, and “x or y” returns y if x is false. See Boolean Operators for a detailed explanation. It’s short, but not very explicit.
explicit 是「明確的」的意思。