python namespace: __main__.Class not isinstance of package.Class

前端 未结 3 2134
轮回少年
轮回少年 2021-01-12 08:41

Consider you have two python files as defined below. Say one is a general package (class2), and the other one does specific overrides and serves as the executab

3条回答
  •  感动是毒
    2021-01-12 09:15

    Thanks for the hints, which finally helped me to experiment in the right direction. The solution I just found in this toy model, is to fix the namespace issue using an import. In order to rule out the issue user4815162342 pointed out, I added another class to class1. The following code for class1.py appears to do, what I want:

    #!/usr/bin/python
    class Test(object):
        pass
    
    class Toast(object):
        pass
    
    class Verificator():
        def check(self, myObject):
            if not isinstance( myObject, Test ):
                print "NOPE: %s is no instance of %s" % (type(myObject),Test)
            else:
                print "OK: %s is instance of %s" % (type(myObject),Test)
    
    if __name__ == '__main__':
        from class2 import getTest
        from class1 import Test, Toast
    
        v = Verificator()
        t = Test()
        v.check(t)
        t = getTest()
        v.check(t)
        t = Toast()
        v.check(t)
    

提交回复
热议问题