Lament: Import is not very smart.

Steve lonetwin at gmail.com
Wed Jul 14 15:15:57 CEST 2004


Hi,
> Here's the problem:
> 
> >> import foo.bar
> >> import bar
> >> foo.bar == bar
> False

This might seem like a problem in this specific case, for this
specific problem, but consider this:

>>> import foo.commonlib
>>> import commonlib

where commonlib is a module I happened to write before I knew that
foo.commonlib existed (which does something entirely different but is
coincidently named in the similar manner.)

I'd be *very* grateful if 
>>> foo.commonlib != commonlib

Regards
Steve




On 13 Jul 2004 15:25:50 -0700, Lonnie Princehouse
<fnord at u.washington.edu> wrote:
> I don't know if this is a bug or a feature, but it sure is annoying.
> I apologize if it's already been discussed to death elsewhere (I
> couldn't find anything).
> 
> Suppose that you've a package named "foo" which contains module "bar".
>  Because you're careless, you've also put foo/ into your path...
> 
> Here's the problem:
> 
> >> import foo.bar
> >> import bar
> >> foo.bar == bar
> False
> 
> The second import doesn't recognize that "bar" is already loaded as
> foo.bar, and loads the module again.  I know it's easy to avoid this
> by being consistent, but wouldn't be more intuitive if import checked
> a hash of loaded module filenames before importing something anew?
> E.g.
> 
> >> import foo.bar
> >> import bar
> >> foo.bar == bar
> True
> 
> It's probably too late to change, but it's food for thought.
> 
> (addendum: I ran into this when isinstance seemingly malfunctioned
> while I was patching together lots of modules into a cohesive package;
> turns out there were some instances that had been created from
> package.module.class and others that were instances of module.class..
> it took a while to figure out just what the problem was)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list