[Python-Dev] Package ambiguities

Andy Robinson andy@robanal.demon.co.uk
Wed, 07 Jun 2000 20:48:48 GMT


We hit some very weird behaviour recently while setting up a package
hierarchy.  Robin Becker managed to distil this into a simple example.
Can anyone shed any light on what is happening below?  Is Python
behaving as it should?

Create a package A, empty __init__.py, with modules as follows:
--------parent.py----------
class Parent:
    pass

--------child.py------------
from parent import Parent
class Child(Parent):
    pass

---------test.py--------
from parent import *
class Examiner:
    def examine(self, arg):
        print 'examining argument:'
        print 'class of arg =3D', arg.__class__
        print 'bases of arg =3D', arg.__class__.__bases__
        print 'arg =3D', arg
        if isinstance(arg, Parent):
            print "arg is an instance of Parent"
        else:
            print "arg IS NOT an instance of Parent"
        print
       =20
if __name__=3D=3D'__main__':
        from traceback import print_exc
        import sys
        def run0():
                from A.child import Child
                e =3D Examiner()
                e.examine(Child())

        def run1():
                from A.child import Child
                from A.test import Examiner
                e =3D Examiner()
                e.examine(Child())
        run0()
        run1()


Running this script produces the following output:

  C:\users\andy\A>test.py
  examining argument:
  class of arg =3D A.child.Child
  bases of arg =3D (<class A.parent.Parent at 7f9150>,)
  arg =3D <A.child.Child instance at 7f9410>
  arg IS NOT an instance of Parent

  examining argument:
  class of arg =3D A.child.Child
  bases of arg =3D (<class A.parent.Parent at 7f9150>,)
  arg =3D <A.child.Child instance at 7f83c0>
  arg is an instance of Parent


Many thanks,

Andy Robinson