Here are two files, foo.py and bar.py bar.py has...
from foo import *
...at the top. bar.py uses types defined n foo.
When importing bar.py from foo I am having trouble determining the types of objects. Looking at the example below why do the calls to isinstance return False? How can I check if these types are the same?
Thanks,
===== foo.py =====
#!/usr/bin/env python
class Spam(object):
def __init__(self, x):
self.x = x
def funcA(self):
print 'function a'
def __str__(self):
return 'Spam object %s' % repr(self.x)
class Eggs(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def funcB(self):
print 'function b'
def __str__(self):
return "Eggs object (%s, %s, %s)" % (repr(self.x), repr(self.y), repr(self.z))
def main(fname):
if not fname.endswith('.py'):
raise Exception("Must be a .py file")
module = __import__(fname[:-3])
for item in module.DATA:
if isinstance(item, Spam):
item.funcA()
elif isinstance(item, Eggs):
item.funcB()
print item
if __name__ == '__main__':
import sys
for fname in sys.argv[1:]:
main(fname)
sys.exit(0)
===== bar.py =====
from foo import *
DATA=[
Spam("hi"),
Spam("there"),
Eggs(1, 2, 3),
]
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)