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


consider have 2 python files defined below. 1 general package (class2), , other 1 specific overrides , serves executable (class1).

class1.py:

#!/usr/bin/python class test(object):     pass  class verificator():     def check(self, myobject):         if not isinstance( myobject, test ):             print "%s no instance of %s" % (type(myobject),test)         else:             print "ok!"  if __name__ == '__main__':     class2 import gettest      v = verificator()     t = test()     v.check(t)     s = gettest()     v.check(s) 

class2.py:

from class1 import test def gettest():     return test() 

what happens first check ok, second fails. reason t __main__.test whereas s class1.test , v.check() checks __main__.test, @ end of day same class, right?

is there way write v.check() such accepts class1.test objects, or other way solve this?

if plan import class1.py elsewhere, move top-level code (if __name__ == '__main__': ... separate file altogether. way both main file , class2 work same class1.test class.

doing else opens can of worms. while can work around immediate problem switching isinstance type(myobject).__name__ == ..., fact remains python process contains 2 test classes there should one. otherwise indistinguishable classes know nothing of each other , fail each other's issubclass tests. practically guarantees hard-to-diagnose bugs further down line.

edit
option explicitly import classes class1 when executing main, in answer. advisable go 1 step further , make sure classes aren't defined in double. example, can move if __name__ == '__main__' block beginning of file, , end sys.exit(0):

if __name__ == '__main__':     import class1, class2     ... use public api module prefixes ...     sys.exit(0)  # rest of module follows here 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -