Hard times with packages and instances

Fredrik Lundh fredrik at pythonware.com
Fri May 6 02:36:10 EDT 2005


Kay Schluehr wrote:

> > if you manage to import the same thing multiple times, you'll have
> > multiple class objects representing the same source code, and is-
> > instance won't work properly.
>
> Importing a class/module multiple times does not cause the problem.
> Writing
>
>   import ForeignPackage.B as B1
>   import ForeignPackage.B as B2

that doesn't import it more than once; Python uses a cache to hold
modules, but the cache only works if Python can be sure that the
modules are the same thing, based on the information in the *import*
statement.  in this case, "ForeignPackage.B" is clearly the same thing
as "ForeignPackage.B".

if you change the path around (in your case, by adding MyPackage to
the path, rather than relying on path-relative import), you'll introduce
multiple ways to import the same module, and the cache won't work
properly.  and when the cache doesn't work, isinstance breaks down
for the reason I stated.

> Flattening the package structure would solve the problem as well as
> destroying all file-system based packages and abandon them from Python.

well, packages for just fine for me.  if you cannot get them to work,
maybe you should stop doing things that don't work.

</F>






More information about the Python-list mailing list