Manual:Coding conventions/Python/fr

Cette page contient les règles de codage  pour les projets Python faisant partie du projet MediaWiki lui-même et de ceux qu'il supporte.

Préambule
Vous devez vous rappeler d'abord que les règles de codage ne sont que des indications pouvant être outrepassées s'il existe ue bonne raison.


 * Visez la lisibilité et l'évidence plutôt que la stricte adhérence dans le souci de respecter strictement.
 * Le code est lu beaucoup plus souvent qu'il n'est écrit.
 * Soyez cohérent avec le code existant et utilisez votre meilleure appréciation. Si ce n'est pas trop difficile de modifier le code existant, soyez gentil.

Pour tout ce qui ne se trouve pas dans ce document, voir la Proposition 0008 d'extension de Python (PEP8) qui décrit l'utilisation générale. Les sections suivantes sont pour la plupart un résumé des sections qui référencent le plus la PEP8.

Version Python
The minimum supported version is 2.7, but in special cases it is ok to support older versions.

If you have not already, you should change to Python 3 for local development.

Caractère espace
Lines should be indented with 4 spaces.

Lines at the end of files should end with a newline, just like every other line in the file.

Try to keep lines under 80 characters long, but aim for readability and obviousness over strict adherence for the sake of strict adherence. Shorter lines are just a general side effect of good idiomatic python - short but properly scoped descriptive names, avoiding staircase code, etc. When splitting up lines, pick the most obviously unambiguous method possible for the situation.

Structure des modules
The standard way to distribute python modules is to create a  file and leverage a library called "distribute". There are modules that will generate the structure of a base project for you, a deprecated one is  which is no longer maintained. A replacement is pythong.

In general module structure should look like this: newproject ├── bin ├── distribute_setup.py ├── docs ├── newproject │    └── __init__.py ├── setup.py └── tests ├── __init__.py      └── newproject_tests.py

Les import
Within a file it's generally a good idea to organize your imports in some fashion. Typically alphabetical order is favored, but this can become unwieldy when importing a large number of libraries. To help avoid this, it's good to separate out imports in this fashion, with each block separated from the others by an empty line:


 * Standard library imports
 * Third party imports
 * Your library imports

Here are some patterns to avoid:

Importation des exemples développés
Here is a more detailed abstracted version (the comments are just for explanation purposes):

Docstrings et l'annotation des fonctions
Generally all but the simplest functions should have docstrings. These are standardized in PEP 257

This makes it possible to automatically generate docs, as well as use Python's built-in  function.

In Python 3.3 and above PEP 3107 specifies syntax for function annotations.

Function annotations do not have a completely set use case, but a common emerging case is for improved help docs and for type annotation.

Conflicts de nommage
Conflicting with builtins is a somewhat common problem. There are some builtin names (like  and  ) that you may want to use in your code. The PEP8 way to deal with these conflicts is by appending an underscore to the name, such as  or   (although if you're naming a variable   that may be a code smell).

If you find yourself in conflict with the name of some part of another module,  is your friend.

Voir aussi

 * Manual:Pywikibot/Development/Guidelines
 * Official WebSite