You say “import mymod”. Where does Python look? Check sys.path, a list of directory names (strings) where it searches. By default: – Current dir (empty string)– Standard library– site-packages The first match wins, which can lead to unpleasant surprises — be careful!
In Python, import always defines a variable. But it only loads the module once. sys.modules, a dict, tracks already-loaded modules: – keys are strings, the module names– values are module objects If you say “import pandas as pd”, sys.modules has a key ‘pandas’, not ‘pd’.
“sys” is where Python keeps vital info about its runtime environment. So why import it? Not to load it; sys is loaded when Python starts. Rather, the import just defines “sys” as a global variable. Importing “sys” takes almost no time, and provides useful info.
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]