Python/it: Difference between revisions

From FreeCAD Documentation
(Created page with "Vedere Introduzione a Python per conoscere il linguaggio di programmazione Python, quindi Guida agli Script Python e Script di base per FreeCAD per iniziare a creare script in FreeCAD .")
(Created page with "== Leggibilità ==")
Line 13: Line 13:
Vedere [[Introduction_to_Python/it|Introduzione a Python]] per conoscere il linguaggio di programmazione Python, quindi [[Python_scripting_tutorial/it|Guida agli Script Python]] e [[FreeCAD_Scripting_Basics/it|Script di base per FreeCAD]] per iniziare a creare script in FreeCAD .
Vedere [[Introduction_to_Python/it|Introduzione a Python]] per conoscere il linguaggio di programmazione Python, quindi [[Python_scripting_tutorial/it|Guida agli Script Python]] e [[FreeCAD_Scripting_Basics/it|Script di base per FreeCAD]] per iniziare a creare script in FreeCAD .


== Readability ==
<span id="Readability"></span>
== Leggibilità ==


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 [https://www.python.org/dev/peps/pep-0008/ PEP8: Style Guide for Python Code] and [https://www.python.org/dev/peps/pep-0257/ PEP257: Docstring Conventions].
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 [https://www.python.org/dev/peps/pep-0008/ PEP8: Style Guide for Python Code] and [https://www.python.org/dev/peps/pep-0257/ PEP257: Docstring Conventions].

Revision as of 10:17, 27 August 2023

FreeCAD è stato originariamente progettato per funzionare con Python 2.x. Questa serie si è conclusa con il rilascio 2.7.18 datato 20 aprile 2020 ed è seguita da Python 3. L'ulteriore sviluppo di FreeCAD sarà effettuato esclusivamente con Python 3 e la compatibilità con le versioni precedenti non sarà supportata.

Descrizione

Python è un linguaggio di programmazione di alto livello per scopi generali, comunemente utilizzato in applicazioni di grandi dimensioni per automatizzare alcune attività creando script o macro.

In FreeCAD, il codice Python può essere utilizzato per creare vari elementi in modo programmatico, senza la necessità di fare clic sull'interfaccia utente grafica. Inoltre, molti strumenti e ambienti di lavoro di FreeCAD sono programmati in Python.

Vedere Introduzione a Python per conoscere il linguaggio di programmazione Python, quindi Guida agli Script Python e Script di base per FreeCAD per iniziare a creare script in FreeCAD .

Leggibilità

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:

Conventions

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

This is a typical function signature

Wire = make_wire(pointslist, closed=False, placement=None, face=None, support=None)
  • Arguments with key-value pairs are optional, with the default value indicated in the signature. This means that the following calls are equivalent:
Wire = make_wire(pointslist, False, None, None, None)
Wire = make_wire(pointslist, False, None, None)
Wire = make_wire(pointslist, False, None)
Wire = make_wire(pointslist, False)
Wire = make_wire(pointslist)
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:
Wire = make_wire(pointslist, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, closed=False, face=None, placement=None)
Wire = make_wire(pointslist, placement=None, closed=False, face=None)
Wire = make_wire(pointslist, support=None, closed=False, placement=None, face=None)
  • Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma.
p1 = Vector(0, 0, 0)
p2 = Vector(1, 1, 0)
p3 = Vector(2, 0, 0)
Wire = make_wire([p1, p2, p3], closed=True)
  • 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.
a_list = [1, 2, 3,
          2, 4, 5]

Wire = make_wire(pointslist,
                False, None,
                None, None)
  • Functions may return an object that can be used as the base of another drawing function.
Wire = make_wire(pointslist, closed=True, face=True)
Window = make_window(Wire, name="Big window")

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 import instruction.

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

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

import FreeCAD as App
import Draft

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1, 1, 0)
p3 = App.Vector(2, 0, 0)
Wire = Draft.make_wire([p1, p2, p3], closed=True)
import FreeCAD as App
import Draft
import Arch

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1, 0, 0)
p3 = App.Vector(1, 1, 0)
p4 = App.Vector(0, 2, 0)
pointslist = [p1, p2, p3, p4]

Wire = Draft.make_wire(pointslist, closed=True, face=True)
Structure = Arch.make_structure(Wire, name="Big pillar")