Languages/Python
|
Python is a general purpose programming language with focus on code readability. It's open-source and free to use also for commercial projects. It's released under it's own PSF license, which allows it to be used in combination with GPL licensed code. Currently there exists a version 2.x.x and 3.x.x. We recommend using the newest 2.x.x version, as lots of modules are not yet ported to 3.x.x.
Check out the Python Tools page to find out how to install Python and some useful modules on your computer.
Read on to get some information about the language Python.
Modules
There exist a lot of modules to extend the basic functionality of Python. A module is a file containing classes and/or functions. For example there are modules to do numerical computation similar to Octave or Matlab or others for symbolic mathematics. You can also define your own modules. Python finds the files if they have the extension .py and if they are placed in the same folder as the script you run, or in the Scripts folder of the Python installation.
A module can be imported into your script by using the name of the file without extension:
import myModule
Elements of the module can then be accessed in your script with the corresponding namespace
myModule.myFunction()
If you do not like to use the module name each time you access the function, you can also import the function implicitly.
from myModule import myFunction myFunction()
In general it is not recommended to use this approach, as the code might get confusing when there exists multiple functions with the same name. Furthermore the code is easier to maintain and reuse if the origin of each function is clearly specified.
To prevent you from writing long module names all the time, an alias can be given to the module
import myModule as mM mM.myFunction()
It's a good coding practice to put all import
statements at the top of the file.
More detailed and complete information about modules can be found in the python documentation.
Packages
When you like to group multiple modules together, they will form a package. Furthermore this adds an additional namespace on import
import myPackage myPackage.myModule.myFunction() myPackage.myOtherModule.myOtherFunction()
or
import myPackage.myModule import myPackage.myOtherModule myModule.myFunction() myOtherModule.myOtherFunction()
To from a package all module files have to put in a subfolder with the name of the package. Then there has to be an __init__.py file in that subfolder. Further subpackages can be created by grouping modules in further subfolders.
More detailed and complete information about packages can be found in the Python documentation.