Languages/Python

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 
{{TOC right}}
 
{{TOC right}}
 
[http://www.python.org/ Python] is a general purpose programming language with focus on code readability.  
 
[http://www.python.org/ 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 [http://docs.python.org/2/license.html PSF license], which allows it to be used in combination with [https://gnu.org/licenses/gpl.html 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 (libraries) are not yet ported to 3.x.x.
+
It's open-source and free to use also for commercial projects. It's released under it's own [http://docs.python.org/2/license.html PSF license], which allows it to be used in combination with [https://gnu.org/licenses/gpl.html 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 [[Tools/Python_Tools]] page to find out how to install it on your machine.
+
== Modules ==
 +
 
 +
There exist a lot of modules to extend the basic functionality of [http://www.python.org/ Python]. Modules are files containing classes and functions. For example there are modules to do numerical computation similar to [[Tools/Octave_Matlab|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.
 +
 
 +
They can be imported into your script by using the name of the file without extension:
 +
 
 +
<source lang="Python">
 +
import myModule
 +
</source>
 +
 
 +
Elements of the module can then be accessed in your script
 +
 
 +
<source lang="Python">
 +
myModule.myFunction()
 +
</source>
 +
 
 +
If you do not like to use the module name each time you access the function, you can also import the function implicitly.
 +
 
 +
<source lang="Python">
 +
from myModule import myFunction
 +
myFunction()
 +
</source>
 +
 
 +
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
 +
 
 +
<source lang="Python">
 +
import myModule as mM
 +
mM.myFunction()
 +
</source>
 +
 
 +
Check out the [[Tools/Python_Tools]] page to find out how to install [http://www.python.org/ Python] and some useful modules on your computer.
  
 
[[Category:Languages]] [[Category:Python]]
 
[[Category:Languages]] [[Category:Python]]

Revision as of 09:35, 8 May 2013

Contents

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.

Modules

There exist a lot of modules to extend the basic functionality of Python. Modules are files containing classes and 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.

They 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

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()

Check out the Tools/Python_Tools page to find out how to install Python and some useful modules on your computer.

Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox