Find the version of Python: To use it in a program, you want version_info:
Want a Python Enum, but don’t care about the values? Use auto: You can now use Days.SUN and Days.MON. With auto, you care the Enum’s values are different, not what they are.
Want to iterate over a Python Enum? __members__ returns a dict-like object, and it supports items(): SUN: 1MON: 2TUE: 3[etc]
Normally, Python doesn’t enforce uniqueness on Enum values: The @unique decorator enforces:
Want your Python Enum class to be sortable? The regular Enum doesn’t do it, but IntEnum does: Days.SUN < Days.THU # True!Days.MON > Days.WED # False!
Enums are a great addition to Python: They print nicer and are symbolic! But… not sortable (yet).
Can you get Python to enforce constants? Yes, with type checking: Ruff won’t complain. Instead, type it as Final and run mypy: mypy myprog.pymyprog.py:6: error: Cannot assign to final name “NAME” [misc]
About immutable vs. constant in Python: – Immutable: The value cannot change. If you own an immutable shirt, you can’t change its color, or add/remove a button.– Constant: Once assigned, a name will always refer to a value. But: Python’s constants are unenforced conventions!
Want the unique key-value pairs from two Python dicts? dict.items() returns a set-like object: This returns the unique key-value pairs: {(‘b’, 20), (‘a’, 10), (‘d’, 40), (‘c’, 30)}
Want the unique keys from two Python dicts? dict.keys() returns a set-like object, supporting set operators: