in Education by
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)

1 Answer

0 votes
by
Have you tried printing Spam, Eggs and the type of item? Spam is Eggs is type of item is Spam object 'hi' type of item is Spam object 'there' type of item is Eggs object (1, 2, 3) The foo.py module is run twice, once as the main program and once when it's imported by bar.py. In the main program, Spam and Eggs are defined as __main__.Spam and __main__.Eggs. In an imported module, Spam and Eggs are defined as foo.Spam and foo.Eggs. __main__.Spam != foo.Spam, __main__.Eggs != foo.Eggs.

Related questions

0 votes
    How can I check a given object is of a given type? How can I check that a object inherit a given type? There's ... that it's a string? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    How do I import other files in Python? How exactly can I import a specific python file like import file.py? How ... gap from extra.py? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    (a) What will be the output of the following Python code? from math import factorial print (math.factorial (5) ... : print(factorial(5) Select the correct answer from above options...
asked Dec 17, 2021 in Education by JackTerrance
0 votes
    If the size of the array used to implement a circular queue is MAX_SIZE. How rear moves to traverse inorder ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    In HTML5 when we want to fill shape by radial gradient we use: ctx.createRadialGradient(centerX1, centerY1, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    One Indian and four American men and their wives are to be seated randomly around a circular table. Then, the conditional ... {1}{5}\) Select the correct answer from above options...
asked Nov 19, 2021 in Education by JackTerrance
0 votes
0 votes
    Rectangular waveguides dominate the circular waveguides. Find the reason. (a) Low cut-off frequency ... by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    A circular disc of radius 5m with a surface charge density ρs = 10sinφ is enclosed by ... proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    The circular waveguides use which function in the frequency calculation? (a) Laplace function (b) ... by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 5, 2021 in Education by JackTerrance
0 votes
    I have a fixed list of services in a Linux server. I want to check the status of these services ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
...