This is the P2PU Archive. If you want the current site, go to www.p2pu.org!
You haven't posted any discussions yet.
Chapter 2 was part of the sign-up task so hopefully you've all been through it. Just to let you know, don't be freaked out if you didn't understand the "first program"- it's not really meant to be your first ever python program so much as a showcase for things we learn later on in the book. Mark goes through and explains each portion of that program in the following chapters. If you have any other questions/comments in chapter 2, you can comment in this thread.
I'm little confused about __name__ and __main__. If i understand correctly __main__ only changes when you're inside a module so how can you define modules?? Are modules like classes?
Yup, his explanation of that was confusing to me too.
Any time you want to call methods from another python file, you will have to import that file. A module is *any* python file that you import into another python file. The problem is that when you import module_name (where module_name.py is the python file) it will actually *run* that python file in the process of importing it. I would recommend trying this yourself just to get a feel for it; try 1) deleting if __name__ == "__main__": in odbchelper.py and un-indenting the statements following it and 2) going into your python prompt (I really like ipython), and doing "import obdchelper". If you compare that to what happens with the file as written, you can see why that might be undesirable if all you wanted to do was call a method that was in obdchelper.
The reason he includes it so early on in the book even though it's a bit complicated is that it is standard practice to include the main block to be executed within an "if __name__ == "__main__":" statement. This is somewhat similar to the style of some other languages, such as Java, which requires a "main" method. Unlike Java, however, you don't *need* to have that block; it's just safer to always do that. The reason it's safer is because any python file can be a module, so doing this allows it to act safely as both a runnable file and as a module.
However, it's perfectly fine to just not bother with it for early things you write; it will make more sense later on as we get more comfortable with this "everything is an object" thing.
Thanks for the time to clear this to me. I never would have thought python does this.