None & Truthiness
None: ค่า “ไม่มีอะไร” หนึ่งเดียวของ Python
หัวข้อที่มีชื่อว่า “None: ค่า “ไม่มีอะไร” หนึ่งเดียวของ Python”TypeScript มีค่า “ไม่มี” อยู่สองตัวคือ null กับ undefined แถมมีกฎ linting ทั้งชุด (strictNullChecks) ไว้คุมอีกที ส่วน Python มีตัวเดียวคือ None ทำหน้าที่เป็นทั้ง sentinel ที่แปลว่า “ไม่มีค่า” และเป็นค่าที่ฟังก์ชัน return กลับมาเองโดยปริยายเมื่อไม่ได้เขียน return อะไรไว้
คุณเปรียบเทียบกับ None ด้วย is (ไม่ใช่ ==) — รายละเอียดเพิ่มเติมอยู่ด้านล่าง
None เทียบกับ null / undefined
หัวข้อที่มีชื่อว่า “None เทียบกับ null / undefined”// TypeScriptlet user: string | null = null;let config: string | undefined = undefined;
// Nullish coalescingconst name = user ?? "Guest";
// Optional chainingconst len = user?.length ?? 0;
// Type narrowingif (user !== null && user !== undefined) { console.log(user.toUpperCase());}# Pythonuser: str | None = None # str | None = Optional[str]
# Fallback with "or"name = user or "Guest"
# Conditional expression (ternary)length = len(user) if user is not None else 0
# Type narrowingif user is not None: print(user.upper())Truthiness: ค่า falsy ของ Python
หัวข้อที่มีชื่อว่า “Truthiness: ค่า falsy ของ Python”กฎ falsy ของ Python คล้ายกับ JavaScript แต่เข้มงวดและสม่ำเสมอกว่า container ที่ว่างเปล่า ใด ๆ เป็น falsy ส่วน ตัวเลขที่ไม่ใช่ศูนย์ ใด ๆ เป็น truthy
| ค่า | Falsy ใน Python? | Falsy ใน JS/TS? |
|---|---|---|
None | ใช่ | null / undefined — ใช่ |
0 | ใช่ | ใช่ |
0.0 | ใช่ | ใช่ |
"" (string ว่าง) | ใช่ | ใช่ |
[] (list ว่าง) | ใช่ | ไม่ — [] เป็น truthy ใน JS |
{} (dict ว่าง) | ใช่ | ไม่ — {} เป็น truthy ใน JS |
set() (set ว่าง) | ใช่ | ไม่มี |
() (tuple ว่าง) | ใช่ | ไม่มี |
สังเกต: array และ object ที่ว่างเปล่าเป็น truthy ใน JavaScript แต่เป็น falsy ใน Python เรื่องนี้ทำให้นักพัฒนา TS หลายคนสะดุดบ่อย ๆ
// TypeScriptconsole.log(Boolean([])); // true — empty array is truthy!console.log(Boolean({})); // true — empty object is truthy!console.log(Boolean(0)); // falseconsole.log(Boolean("")); // falseconsole.log(Boolean(null)); // false
if ([]) console.log("truthy"); // prints!# Pythonprint(bool([])) # False — empty list is FALSYprint(bool({})) # False — empty dict is FALSYprint(bool(0)) # Falseprint(bool("")) # Falseprint(bool(None)) # False
if []: print("truthy") # does NOT printelse: print("falsy") # prints
# Idiomatic: guard against empty containersitems = []if not items: print("No items found")การเช็ค None: ใช้ is ไม่ใช่ ==
หัวข้อที่มีชื่อว่า “การเช็ค None: ใช้ is ไม่ใช่ ==”ใน Python is เช็ค identity (object เดียวกันในหน่วยความจำ) ส่วน == เช็ค equality (ค่าเท่ากัน) สำหรับ None ให้ใช้ is เสมอ — มี object None เพียงตัวเดียวในโปรเซส Python ดังนั้นการเช็ค identity จึงเป็นวิธีที่ถูกต้อง
// TypeScript — use strict equalityfunction process(data: string | null | undefined): void { if (data === null || data === undefined) { console.log("No data"); return; } console.log(data.toUpperCase());}# Python — use "is None" / "is not None"def process(data: str | None) -> None: if data is None: print("No data") return print(data.upper())
# Common pattern: default if Nonedef greet(name: str | None = None) -> str: name = name or "World" # replaces None (or falsy) with default return f"Hello, {name}!"
print(greet()) # Hello, World!print(greet("Alice")) # Hello, Alice!ลองเล่นดู
หัวข้อที่มีชื่อว่า “ลองเล่นดู”# None checksx = Noneprint(x is None) # Trueprint(x is not None) # False
# Truthinessprint(bool([])) # False — empty listprint(bool([1])) # True — non-empty listprint(bool({})) # False — empty dictprint(bool({"a": 1})) # Trueprint(bool("")) # False — empty stringprint(bool("hi")) # Trueprint(bool(0)) # Falseprint(bool(1)) # True
# None in function returndef get_user(user_id: int): if user_id == 1: return {"name": "Alice"} return None # implicit in Python, but explicit is fine
user = get_user(99)if user is None: print("User not found")
user2 = get_user(1)if user2 is not None: print(f"Found: {user2['name']}")Loading Python runtime (first run only)…