[Twisted-Python] python's "import" is giving me a headache.

Glyph Lefkowitz glyph at twistedmatrix.com
Wed Dec 5 00:21:33 EST 2001


So, there's a particular corner of the coding standard for Twisted
currently which is the root of a few annoying problems and has been the
cause of one or two bugs.  It's the module import requirement.

Originally it seemed like a pretty good idea for the following reasons;
if one is using packages, it is easiest to figure out where the code is
coming from if you name the module explicitly.  Documentation generators
document modules individually, and so that's how people would learn to
import modules.  Accessing the module indirectly, and not the class,
allows for reloading to work naturally and different modules to have
similarly named classes when appropriate.

In pracitce, it doesn't work out quite so well.  It did not occur to me
at the time how hard it would be to distinctly name all modules within
classes, or how often a local variable name would clash with a
straightforwardly chosen package name.

I am considering a change to the coding standard (and the attendant
massive refactoring) to a standard where modules "promote" public
classes and functions to the module level.  For example, in
twisted/words/service.py:

# promote public interface
from twisted import python
import twisted.words
twisted.python.publicInterface(twisted.words,
    Service,
    WordsClientInterface,
    Participant,
    ...)
# end

The end user would probably then have to do this in order to use that
module:

import twisted.words.service
from twisted import words
words.Service(...)

Pros:

* fewer names to worry about clashing with
* nested modules less inconvenient
* only public portions of interface present at package level

Cons:

* it doesn't work that way now and it would require work to change
* you still have to know which module to import
* it requires manual declaration of public interface

The number of conflicts both between modules and convenient variable
names is increasing with time, and I think that something has to be done
about it, but I don't know if this is an appropriate solution.  At any
rate, such a change will likely affect most Twisted developers, so I'd
like to hear feedback before I do anything.

-- 
______      you are in a maze of twisted little applications, all
|   |_\     remarkably consistent.
|     |          -- glyph lefkowitz, glyph @ twisted matrix . com
|_____|             http://www.twistedmatrix.com/






More information about the Twisted-Python mailing list