Bringing two files together - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Bringing two files together (/thread-13384.html) |
Bringing two files together - tjnichols - Oct-12-2018 I have 2 files that I'm trying to join. I've tried this with the files below. I've also tried this with the files provided by the instructor - still no dice. I've set this up in a virtual environment so I would have more control in what was installed. class A: def spam(self): print('A.spam') from .a import A class B(A) def bar(self): print('B.bar') from .a import A from .b import BThis is the error I get: As always - your help is most appreciated!
RE: Bringing two files together - ThiefOfTime - Oct-12-2018 I suspect that the class A is in a file named a.py and the class B is in a file named b.py, right? remove the . infront of the a and b. What it does right now is that it looks in the __main__ module where it doesn't find the files a and b. Where did you place the files? Are they in the same directory? If so, just write: from a import A class B(A) def bar(self): print('B.bar') RE: Bringing two files together - tjnichols - Oct-12-2018 I must be missing something because I've done as you instructed. from a import A class B(A) def bar(self): print('B.bar')and this is the error I get. I also tried copying yours - still nada. Is there a plugin I might be missing?Again - thanks for your help! RE: Bringing two files together - Larz60+ - Oct-12-2018 import a class B: def __init__(self): self.a = a.A() def bar(self): print('B.bar') def bspam(self): self.a.spam() if __name__ == '__main__': b = B() b.bspam()output:
RE: Bringing two files together - nilamo - Oct-12-2018 (Oct-12-2018, 02:21 PM)tjnichols Wrote: I must be missing something because I've done as you instructed. The error explains what the issue is. class B(A) is a syntax error. You need a colon at the end of that line.
RE: Bringing two files together - tjnichols - Oct-12-2018 Lars60+ - I'm sorry I missed your post. import a class B: def __init__(self): self.a = a.A() def bar(self): print('B.bar') def bspam(self): self.a.spam() if __name__ == '__main__': b = B() b.bspam()This is so much more than what is in the book. It was written by David Beazley so I know it's old because he is writing a newer edition. Can you point me in the direction of where you learned this so I can learn how and why you did it this way? As always - I appreciate your help! RE: Bringing two files together - tjnichols - Oct-12-2018 nilamo - that did it! THANK YOU!!! Lars60+ - I would still like to know where you got the code you posted. I understand you wrote it, but I would be very interested to know where you learned. Thank all of you! RE: Bringing two files together - nilamo - Oct-12-2018 Which part of the code are you curious about? There really isn't anything special there, B is keeping a reference to an instance of A , instead of inheriting from A .
RE: Bringing two files together - Larz60+ - Oct-12-2018 Lars60+ - I would still like to know where you got the code you posted. I understand you wrote it, but I would be very interested to know where you learned. I wrote it. It's standard class layout. to do it the way you were trying, that is to use another class as object of a class, you need to initialize the object within the init of the new class. As far as where I learned how to do this, I don't have a clue, coming from c++ background, I was already familiar with the basic concepts. David Beazley is a Great author, and an even better speaker! You should be able to get his code to work without problems, perhaps re-read the section. |