import and package confusion

Scott David Daniels Scott.Daniels at Acm.Org
Wed Apr 29 19:06:23 EDT 2009


Dale Amon wrote:
> ....
> The point I take away from this is that packages and
> modules have dotted names, but Classes do not and there
> is no way to do exactly what I wanted to do. 
Nope.  You have not been clear with what you want, and part
of the lack of clarity is your imprecision about names.

If you insist on having a class per module, you will
always have redundant-looking class names somewhere.
You will help yourself out a lot by not sharing the class
name and the base class name (not the least in error
messages), but it is possible to have them the same.
If your module name, base class name, specialized name,
and derived class name are all the same, there are _way_
too many chances to confuse yourself about exactly what
you are talking about.
Assuming:
	VLMLegacy/
           __init__.py
           Reader.py        [defines class Reader]
	  Conditions.py    [defines class Conditions]
           ...
           VLM4997/
             __init__.py
               Reader.py        [defines class Reader]
	      Conditions.py    [defines class Conditions]
           WINGTL/
             __init__.py
             Conditions.py
               Reader.py        [defines class Reader]
	      Conditions.py    [defines class Conditions]

After
     import VLMLegacy.Reader
     import VLM4997.Reader
There are four things named Reader:
     VLMLegacy.Reader        # A module
     VLMLegacy.Reader.Reader # A class
     VLM4997.Reader          # a module
     VLM4997.Reader.Reader   # a class

How I'd do it to "sort of" fit your style:

	VLMLegacy/
           __init__.py
           base_reader.py        [defines class BaseReader]
           base_conditions.py    [defines class BaseConditions]
           ...
           VLM4997/
             __init__.py
             reader.py        [defines class Reader]
             conditions.py    [defines class Conditions]
           WINGTL/
             __init__.py
             reader.py        [defines class Reader]
             conditions.py    [defines class Conditions]


I'd make the VLMLegacy/__init__.py file read:
     from VLMLegacy.base_reader import BaseReader
     from VLMLegacy.base_conditions import BaseConditions
     ...
I'd make VLM4997/reader.py read:
     import VLMLegacy
     class Reader(VLMLegacy.BaseReader):
         ...
I'd make VLM4997/__init__.py read:
     from VLMLegacy.VLM997.reader import Reader
     from VLMLegacy.VLM997.conditions import Conditions
     ...

The the consuming python code can do:
     import VLMLegacy.VLM997
     something = VLMLegacy.VLM997.Reader()
     ...

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list