Documentation for importing modules / pythonpath?
Steven Taschuk
staschuk at telusplanet.net
Fri Feb 21 08:37:59 EST 2003
(Just replying to one piece of this.)
Quoth Johannes Eble:
[...]
> Furthermore,
> I am used to the Java way to handle the import /classpath issue. I
> think the main difference is that Java doesn't have modules, it has
> only classes and packages (classes=files=modules if you like it that
> way). If you write 'import <package>' and the package is in your
> classpath then you can use all the classes of that package. In Python
> you have to write 'from <package>.A import A', 'from <package>.B
> import B' and so on for all (class) files in the package to get the
> same effect. [...]
Following the Java way of doing things will be very inconvenient
in Python, as you are discovering.
In Java, you have, for example,
java/
util/
LinkedList.java
HashSet.java
(etc., one file per class)
In Python you'd normally do this instead:
java/
util.py (one file, lots of classes)
Don't think of Java classes as being like Python modules just
because they both consist of a single source file -- that's
incidental. Instead, think of Python modules as being like Java
packages:
- Things in the same Python module can refer to each other without
imports, just like classes in the same Java package can refer to
each other without imports.
- You can import all the things in a module at once with 'from foo
import *', just like you can 'import java.util.*' in Java.
(Packages in Python are a bit like Java packages that have
subpackages.)
> [...] I think that Java is easier to use here if you write only
> one class in one file (which I definitely prefer).
Certainly Java makes things easier if you do things Java's way.
But Java's way is not Python's. (Neither is it C's way: imagine
putting each function in its own .c file and having to #include
"foo.h" for every function you wanted to use. Ick.)
Why do you prefer having one class in one file? (That always
annoyed me in Java.)
--
Steven Taschuk staschuk at telusplanet.net
"What I find most baffling about that song is that it was not a hit."
-- Tony Dylan Davis (CKUA)
More information about the Python-list
mailing list