ruk·si

🐍 Python
Evaluation Time

Updated at 2017-01-02 23:29

You must be aware when the Python interpreter evaluates each block of code. The most important thing in interpreter evaluation is "import time" and "runtime".

Import time: At import time, the interpreter goes through the .py module in one pass from top to bottom and generates bytecode, alerting about any syntax errors. If up-to-date .pyc files exist in local __pycache__, those are used instead.

  • All top-level code is ran at import time.
  • All top-level code of import chain is ran at import time.
  • Function bodies are not ran at import time.
  • Function decorators are ran at import time.
  • Class bodies are ran at import time, even nested classes.
  • Class decorators are ran after the wrapped class body.

Source

  • Fluent Python, Luciano Ramalho