[Tutor] Rule of thumb for imports - Namespaces are important

Charlie Clark Charlie Clark <charlie@begeistert.org>
Mon, 23 Jul 2001 18:19:54 +0200


>I think I'm a little confused about importing modules.  If I were to do
>the below, unless I needed other 'sys' functions, I'd be tempted to use
>"from sys import exit" and "exit()."

Of course you can do that but you might end up writing another function 
called exit. Using import and then qualifying the function you want from the 
module you imported packages it up and makes it easier to work out what the 
function is doing. If nothing else it helps you when look at the code you 
wrote a while back but actually you must do this if you want to be able to 
reload your module.

exit() -> go and look at the top of the module to see where exit is defined 
in the program you're looking at or whether it has been copied into it by a 
"from"

sys.exit() alles klar! We know where this function is from

>I'm assuming the entire module uses more memory than just the one function
>and I would think one should conserve memory.  (Even if one doesn't have
>to - I'm from the "waste not, want not" school.)
I don't know so much about that. Python manages memory for you and unless the 
module has a very large namespace I don't think this would be a problem and 
having clearly readable code is very important and worth the overhead.
>Is there a rule of thumb regarding this issue?
That's my own rule of thumb based on the code I've written from others. I do 
use from x import * for some very special (usually BeOS specific modules) 
when I'm too lazy to write the whole namespace out and I know I'm unlikely to 
write functions with similar names ie.

from BeOS.fsattr import *

gives me read_attrs, write_attr, remove_attr in the local namespace which is 
fine for my own stuff. If I were packaging something up for distribution, I'd 
probably take the trouble of fully qualifying the functions.

Charlie

PS: sorry for the BeOS-bias but extensible file system attributes which can 
be manipulated by Python are just too cool!
-- 
Charlie Clark
Helmholtzstr. 20
Düsseldorf
D- 40215
Tel: +49-211-938-5360
GSM: +49-178-463-6199
http://www.begeistert.org