Hard times with packages and instances

Fredrik Lundh fredrik at pythonware.com
Thu May 5 16:28:50 EDT 2005


Kay Schluehr wrote:

> I wonder why the isinstance() function is sensitive about the import
> path i.e. the result depends not only on the class and the instance but
> also on how a class is imported?

isinstance uses class object identity.

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.

> Example:
>
> MyPackage/ Top-level package
>       __init__.py Initialize package
>       __me__.py                 Module used for setting Python-path
>       A.py                      Use objects of ForeignPackage and
>                                 subpackages
>       ForeignPackage/ Don't touch!
>             __init__.py         Initialize package
>             B.py                Defines class B1
>       MySubPackage/ Subpackage
>             __init__.py         Initialize subpackage
>     C.py                Defines instance checker for B1 instances

in my newsreader, it looks like the C.py module is defined some-
where inbetween MyPackage and MyPackage/ForeignPackage.
what file system are you using? ;-)

(I don't have the energy to decipher your convoluted import
and path-manipulation code; but my intuition tells me that if
you flatten the hierarchy, put MyPackage and ForeignPackage
at the same level, and stop messing with the path inside the
__init__ files, your problems will just disappear).

</F>






More information about the Python-list mailing list