isinstance() bug

Sidharth Kuruvila sidharthk at hotmail.com
Wed Jan 28 10:42:13 EST 2004


"Michal Vitecek" <fuf at mageo.cz> wrote in message
news:mailman.907.1075296157.12720.python-list at python.org...
> hello,
>
>  please consider the following situation:
>
>  under the current directory there's a subdirectory 'package' with two
>  files: __init__.py and module.py
>
>     ./package:
>         __init__.py
>         module.py
>
>  module.py contains:
>
>  class A(object):
>     pass
>
>  aModule = A()
>
>
>  now, let's do:
>
>  current directory:
>
>     >>> import package.module
>     >>> type(package.module.aModule)
>     <class 'package.module.A'>
>     >>> isinstance(package.module.aModule, package.module.A) # so far good
>     1
>     >>> a = package.module.A()
>     >>> isinstance(a, package.module.A) # so far good
>     >>> import sys
>     >>> sys.path.append('package')
>     >>> import module
>     >>> a = module.A()
>     >>> isinstance(a, package.module.A) # will return 0 !!!
>     0
>     >>> isinstance(package.module.aModule, module.A) # will return 0 !!!
>     0
>     >>>
>
>  how is it possible that it IS important how you imported a class
>  definition for isinstance() to work? it's insane!
>
> --
> fuf (fuf at mageo.cz)
>

its got to do with how python imports modules.
both imports are treated as seperate modules because the relative paths are
diferent.
python is a dynamic language it figures out where modules are located at run
time, it would be expensive to check all paths to see if they ended up at
the same file.





More information about the Python-list mailing list