Skip to content

值与判断

先记住几组「长得不像」的基本值。后面所有 if 都靠它们。

真假与空

PythonJavaScript说明
True / Falsetrue / falsePython 首字母大写
Nonenull / undefinedPython 只有一个空值
and / or / not&& / || / !关键字,不是符号
if not text:if (!text)空串、空列表、None0 都偏假
x is Nonex === null身份判断用 is,不要写 == None
a or ba || b取第一个真值,常当缺省回退
python
if not text:
    return ""

date_compact = date_str or datetime.now().strftime("%Y%m%d")
content_raw = item["content"] or item["description"]
js
if (!text) return ''
const dateCompact = date_str || formatToday()
const contentRaw = item.content || item.description

多值「在不在」

python
if normalized.get(field) in (None, ""):
    ...
js
if ([null, undefined, ''].includes(normalized[field])) {
  ...
}
PythonJavaScript
x in (a, b, c)[a, b, c].includes(x)
x in {"a", "b"}set.has(x)

下一页叠:怎么写函数、怎么导入。