Developing for tomorrow, one dream at a time
So to continue my learning I have decided to pick up Python as my next programming language. The following are my notes, on the accepted practices of writing in Python.
Firstly lets examine the structure of a Python program. Firstly as with any new programming language the first thing to mention is that Python would suggest you should avoid global variables as much as possible. Due to the use of modules and imports in python, you should be able to spit your program up easily into layers. Those layers could be:
Each of those layers can be represented by a single module, or multiple modules. The python styling guide dictates that module names should be kept short, should be lowercase, and not include the characters “?” or “.”. The reason for the latter, is the “.” signifies a subfolder in your import statement and rather than “import new.junk.py” working with the file “new.junk.py” it will rather attempt to work with the file “junk.py” in the sub directory “new”. Underscores are also recommended to be avoided in the filename of your module. Also for the love of God, do not import * in your project. please be specific.
To help you write better programs, I thought it would be prudent for us to discuss the path of execution in python. Firstly python will check if the module exists in the current directory as the caller. If it doesn’t exist it will search in the paths recursively, and raise an ImportError exception if not found.
Once the module is found, the interpreter will execute the module in it’s own scope, not global. Functions and class definitions are stored in the module’s dictionary. The modules variables, functions, and classes are then available through the module’s namespace, unlike C which places included content in the same namespace, python gives us some isolation.
What would life be like, if we didn’t start our programming journey with “Hello World”?!
Create a file named: helloWorld.py
print("Hello World")
Execute the file with the following command:
python helloWorld.py