python名称空间:__main __.Class不是package.Class的实例 [英] python namespace: __main__.Class not isinstance of package.Class

查看:348
本文介绍了python名称空间:__main __.Class不是package.Class的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑您有以下定义的两个python文件.说一个是通用程序包(class2),另一个是特定的替代程序并用作可执行程序(class1).

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 executable (class1).

class1.py:

class1.py:

#!/usr/bin/python
class Test(object):
    pass

class Verificator():
    def check(self, myObject):
        if not isinstance( myObject, Test ):
            print "%s is no instance of %s" % (type(myObject),Test)
        else:
            print "OK!"

if __name__ == '__main__':
    from class2 import getTest

    v = Verificator()
    t = Test()
    v.check(t)
    s = getTest()
    v.check(s)

class2.py:

class2.py:

from class1 import Test
def getTest():
    return Test()

发生的事情是,第一个检查是正确的,而第二个检查是失败的.原因是t__main__.Test,而sclass1.Test,并且v.check()检查__main__.Test,但是最终还是属于同一类,对吗?

What happens is that the first check is OK, where the second fails. The reason is that t is __main__.Test whereas s is class1.Test and v.check() checks for __main__.Test, but at the end of the day it is the same class, right?

是否可以编写v.check()使其也接受class1.Test对象,或者采用其他任何方法来解决此问题?

Is there a way to write v.check() such that it also accepts class1.Test objects, or any other way to solve this?

推荐答案

如果计划从其他位置导入class1.py,请将顶级代码(if __name__ == '__main__': ...)一起移动到单独的文件中.这样,两个主文件和class2使用相同的class1.Test类.

If you plan to import class1.py from elsewhere, move the top-level code (if __name__ == '__main__': ... to a separate file altogether. That way both the main file and class2 work with the same class1.Test class.

几乎执行其他所有操作都会打开一罐蠕虫.尽管您可以通过将isinstance切换为type(myObject).__name__ == ...来解决紧迫的问题,但事实是您的Python进程包含两个Test类,其中应该只有一个.否则无法区分的类彼此一无所知,并且彼此之间的issubclass测试失败.这实际上保证了难以诊断的错误.

Doing almost anything else opens a can of worms. While you can work around the immediate problem by switching isinstance to type(myObject).__name__ == ..., the fact remains that your Python process contains two Test classes where there should be only one. The otherwise indistinguishable classes know nothing of each other and fail each other's issubclass tests. This practically guarantees hard-to-diagnose bugs further down the line.

编辑
另一种选择是像以您的答案一样,在以main身份执行时从class1显式导入类.建议更进一步,并确保不要将类定义为双重类型.例如,您可以将if __name__ == '__main__'块移动到文件的开头,并以sys.exit(0):

EDIT
Another option is to explicitly import the classes from class1 when executing as main, as in your answer. It would be advisable to go one step further and make sure that the classes aren't defined in double. For example, you can move the if __name__ == '__main__' block to the beginning of the file, and end it with sys.exit(0):

if __name__ == '__main__':
    import class1, class2
    ... use only the public API with module prefixes ...
    sys.exit(0)

# the rest of the module follows here

这篇关于python名称空间:__main __.Class不是package.Class的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆