1

I'm preparing a package for PyPI submission with a similar (simplified) structure. Trying to balance memory footprint, execution time, and package design/dependencies.

enter image description here

Is there a Pythonic or some optimal placement (and why) of from math import exp?

  1. at the top of main.py
  2. at the top of each sub-module (shared.py, a.py, b.py)
  3. at the top of each class
  4. at the top of each method that uses exp()
  5. some other ways to import?

A gentle/constructive criticism of (or inquiries about) the structure is welcome :) References would be great!

Oleg Melnikov
  • 3,080
  • 3
  • 34
  • 65
  • 1
    You should put it at the top of every script where `exp` is used in any class/method/function - imports are cached, so there's no performance issue, and having it explicitly imported in every namespace aids readability. – jonrsharpe Nov 20 '15 at 21:00
  • Thanks. This makes sense and is similar to what I thought. Also, if the `exp` import is at UDF level, is it reimported with every function call? – Oleg Melnikov Nov 20 '15 at 21:24
  • UDF? After the first `import`, it's just a cached lookup. – jonrsharpe Nov 20 '15 at 21:26
  • UDF :) https://en.wikipedia.org/wiki/User-defined_function – Oleg Melnikov Nov 20 '15 at 21:58
  • Found it: According to python.org, UDF-level imports are not re-executed with each function call, but imports are still checked (against `sys.modules`, I suppose) : https://wiki.python.org/moin/PythonSpeed/PerformanceTips#Import_Statement_Overhead – Oleg Melnikov Nov 20 '15 at 22:03

0 Answers0