Identifier = ชื่อตัวแปร ฟังก์ชัน คลาส โมดูล แพ็กเกจ
กฎ Syntax
- ขึ้นต้นด้วย: ตัวอักษร (a-z, A-Z) หรือ Underscore (
_) - ตามด้วย: ตัวอักษร, ตัวเลข (0-9), Underscore ได้ไม่จำกัด
- Case-sensitive:
name≠Name≠NAME - ห้ามใช้ Keywords
Convention (PEP 8)
| ประเภท | Style | ตัวอย่าง |
|---|---|---|
| Class | CapWords (PascalCase) | MyClass |
| Function/Variable/Module | snake_case | my_function |
| Constant | UPPER_SNAKE_CASE | MAX_SIZE |
| Private (Internal) | Leading Underscore | _internal_var |
| Strong Private (Name Mangling) | Double Leading Underscore | __private |
| Special (Dunder) | Double Leading & Trailing | __init__, __str__ |
ตัวอย่าง Valid/Invalid
# Valid
my_var, _private, MyClass, CONSTANT, var2
# Invalid
2var, my-var, class, for, my.var