I'm a beginner to python, and have written a module that looks something like this:
# filename: mymodule.py
import numpy as np
from datetime import datetime
def a():
...<stuff>...
def b():
...<stuff>...
The consensus in this thread generally (and in agreement with PEP8) seems to be that import statements should be at the file header. However, now when I import mymodule
, running dir(mymodule)
shows that objects np
and datetime
are part of mymodule
-- which offhand, seems inefficient and "sloppy". It seems one way to preserve only classes and defs would be some kind of conditional deletion via dynamic iteration over globals()
(which after trying and failing for a bit, seems really elusive), or just use the del
keyword on everything.
The main question: can I do this, and can I do this dynamically instead of explicitly? Don't the def
s work independently, regardless of whether the header modules are part of the import? Otherwise from <x> import <y>
would break every time, I would think.