I've started on my Python course. So far, the code has been familiar because the first few basic codes are similar to Javascript. And then modules happened. Confusion and despair! What is the world is
From my understanding, when the Python interpreter reads a source file, it first sets the variable __name__ and then it executes all the code in the file. If that particular file that you are running(i.e. your module) is the main program, the interpreter will assign '__name__ = "__main__" '. Thereafter, any code in the aforementioned 'if' statement is run. If you have, instead, imported a module, the interpreter assigns '__name__
= "(name of file - .py)" '. This means the aforementioned code will not run.
It is explained so beautifully here.
Also, I have just discovered a game called 'Pythons and Ladders'. Guess I shouldn't be surprised that I am not the first to refer to Python programming that way :P
'if __name__ == "__main__": 'and why must I reach this section of my course on a public holiday when none of the instructors are in :( Stack overflow to the rescue, providing me a lifeline while I was drowning in a pit of serpents. I feel eternally indebted to a particular Mr Fooz.
![]() |
Picture from here |
= "(name of file - .py)" '. This means the aforementioned code will not run.
It is explained so beautifully here.
Also, I have just discovered a game called 'Pythons and Ladders'. Guess I shouldn't be surprised that I am not the first to refer to Python programming that way :P
Comments
Post a Comment