Python/en

Description
Python is a general purpose, high level programming language that is very commonly used in large applications to automate some tasks by creating scripts or macros.

In FreeCAD, Python code can be used to create various elements programmatically, without needing to click on the graphical user interface. Additionally, many tools and workbenches of FreeCAD are programmed in Python.

See Introduction to Python to learn about the Python programming language, and then Python scripting tutorial and FreeCAD Scripting Basics to start scripting in FreeCAD.

Readability
Readability of Python code is one of the most important aspects of this language. Using a clear and consistent style within the Python community facilitates contributions by different developers, as most experienced Python programmers expect the code to be formatted in a certain way and to follow certain rules. When writing Python code, it is advisable to follow PEP8: Style Guide for Python Code and PEP257: Docstring Conventions.

These documents present explanations in a more user-friendly way:
 * How to Write Beautiful Python Code With PEP 8
 * Documenting Python Code: A Complete Guide.

Conventions
In this documentation, some conventions for Python examples should be followed.

This is a typical function signature


 * Arguments with key-value pairs are optional, with the default value indicated in the signature. This means that the following calls are equivalent:


 * In this example the first argument doesn't have a default value so it should always be included.


 * When the arguments are given with the explicit key, the optional arguments can be given in any order. This means that the following calls are equivalent:


 * Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma.


 * If code needs to be broken over several lines, this should be done at a comma inside brackets or parentheses; the second line should be aligned with the previous one.


 * Functions may return an object that can be used as the base of another drawing function.

Imports
Python functions are stored in files called modules. Before using any function in that module, the module must be included in the document with the instruction.

This creates prefixed functions, that is,. This system prevents name clashes with functions that are named the same but that come from different modules. For example, the two functions and  may coexist without problem.

Full examples should include the necessary imports and the prefixed functions.