Python modules vs classes -
i have following directory structure:
ican/ __init__.py haz/ __init__.py apple.py grape/ __init.py grape.py where apple.py , grape.py have fruit class definitions. in test file, trying load fruit class via akin myapple = ican.haz.apple() or mygrape = ican.haz.grape().
what correct directory structure , import structure loading class of sort? have tried import ican.haz icanhaz , calling myapple = icanhaz.apple(). looking put of classes in same spot , load them via ican.haz.<class>.
to uniform, either
a) apple.py should 1 level lower in apple folder init, or
b) grape.py should 1 level higher , not in own grape folder.
then import from ican.haz.apple import apple , from ican.haz.grape import grape (for option a); or from ican.haz import apple, grape (for option b).
to structure want, go option b , modify init under haz.
haz/__init__.py:
from .apple import apple .grape import grape __all__ = ['apple', 'grape'] and import import ican.haz.* (considered bad style though). , ~mangling module vs class name~ (not more since class names corrected). better use 1 of lines below option or b.
btw, imports according current structure need be:
from ican.haz import apple ican.haz.grape import grape = apple.apple() g = grape()
Comments
Post a Comment