2016-10-30 09:46:26 +00:00
|
|
|
|
# SOME DESCRIPTIVE TITLE.
|
|
|
|
|
# Copyright (C) 1990-2016, Python Software Foundation
|
|
|
|
|
# This file is distributed under the same license as the Python package.
|
|
|
|
|
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
|
|
|
|
#
|
|
|
|
|
#, fuzzy
|
|
|
|
|
msgid ""
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Project-Id-Version: Python 2.7\n"
|
|
|
|
|
"Report-Msgid-Bugs-To: \n"
|
|
|
|
|
"POT-Creation-Date: 2016-10-30 10:44+0100\n"
|
|
|
|
|
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
|
|
|
|
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
|
|
|
|
"Language-Team: LANGUAGE <LL@li.org>\n"
|
|
|
|
|
"MIME-Version: 1.0\n"
|
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\n"
|
|
|
|
|
"Content-Transfer-Encoding: 8bit\n"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:5
|
|
|
|
|
msgid "Programming FAQ"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "FAQ de programmation"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:12
|
|
|
|
|
msgid "General Questions"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Questions générales"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:15
|
|
|
|
|
msgid ""
|
|
|
|
|
"Is there a source code level debugger with breakpoints, single-stepping, "
|
|
|
|
|
"etc.?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Existe-t'il un débogueur de code source avec points d'arrêts, exécution pas-"
|
|
|
|
|
"à-pas, etc. ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:17 ../Doc/faq/programming.rst:60
|
|
|
|
|
msgid "Yes."
|
2017-10-23 20:33:03 +00:00
|
|
|
|
msgstr "Oui."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:19
|
|
|
|
|
msgid ""
|
|
|
|
|
"The pdb module is a simple but adequate console-mode debugger for Python. It "
|
|
|
|
|
"is part of the standard Python library, and is :mod:`documented in the "
|
|
|
|
|
"Library Reference Manual <pdb>`. You can also write your own debugger by "
|
|
|
|
|
"using the code for pdb as an example."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Le module pdb est un débogueur console simple, mais parfaitement adapté à "
|
|
|
|
|
"Python. Il fait partie de la bibliothèque standard de Python, sa "
|
|
|
|
|
"documentation se trouve dans le :mod:`manuel de référence <pdb>`. Vous "
|
|
|
|
|
"pouvez vous inspirer du code de pdb pour écrire votre propre débogueur."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:24
|
|
|
|
|
msgid ""
|
|
|
|
|
"The IDLE interactive development environment, which is part of the standard "
|
|
|
|
|
"Python distribution (normally available as Tools/scripts/idle), includes a "
|
|
|
|
|
"graphical debugger."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"L'environnement de développement interactif IDLE, qui est fourni avec la "
|
|
|
|
|
"distribution standard de Python (normalement disponible dans Tools/scripts/"
|
|
|
|
|
"idle) contient un débogueur graphique."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:28
|
|
|
|
|
msgid ""
|
|
|
|
|
"PythonWin is a Python IDE that includes a GUI debugger based on pdb. The "
|
|
|
|
|
"Pythonwin debugger colors breakpoints and has quite a few cool features such "
|
|
|
|
|
"as debugging non-Pythonwin programs. Pythonwin is available as part of the "
|
|
|
|
|
"`Python for Windows Extensions <https://sourceforge.net/projects/pywin32/"
|
|
|
|
|
">`__ project and as a part of the ActivePython distribution (see https://www."
|
|
|
|
|
"activestate.com/activepython\\ )."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"PythonWin est un environnement de développement intégré (EDI) Python qui "
|
|
|
|
|
"embarque un débogueur graphique basé sur pdb. Le débogueur PythonWin colore "
|
|
|
|
|
"les points d'arrêts et possède quelques fonctionnalités sympathiques, comme "
|
|
|
|
|
"la possibilité de déboguer des programmes développés sans PythonWin. "
|
|
|
|
|
"PythonWin est disponible dans le projet `Extensions Python pour Windows "
|
|
|
|
|
"<https://sourceforge.net/projects/pywin32/>`__ et fait partie de la "
|
|
|
|
|
"distribution ActivePython (voir https://www.activestate.com/activepython\\ )."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:35
|
|
|
|
|
msgid ""
|
|
|
|
|
"`Boa Constructor <http://boa-constructor.sourceforge.net/>`_ is an IDE and "
|
|
|
|
|
"GUI builder that uses wxWidgets. It offers visual frame creation and "
|
|
|
|
|
"manipulation, an object inspector, many views on the source like object "
|
|
|
|
|
"browsers, inheritance hierarchies, doc string generated html documentation, "
|
|
|
|
|
"an advanced debugger, integrated help, and Zope support."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"`Boa Constructor <http://boa-constructor.sourceforge.net/>`_ est un EDI et "
|
|
|
|
|
"un constructeur d'interface homme-machine basé sur wxWidgets. Il propose la "
|
|
|
|
|
"création et la manipulation de fenêtres, un inspecteur d'objets, de "
|
|
|
|
|
"nombreuses façons de visualiser des sources comme un navigateur d'objets, "
|
|
|
|
|
"les hiérarchies d'héritage, la documentation html générée par les "
|
|
|
|
|
"docstrings, un débogueur avancé, une aide intégrée et la prise en charge de "
|
|
|
|
|
"Zope."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:41
|
|
|
|
|
msgid ""
|
|
|
|
|
"`Eric <http://eric-ide.python-projects.org/>`_ is an IDE built on PyQt and "
|
|
|
|
|
"the Scintilla editing component."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"`Eric <http://eric-ide.python-projects.org/>`_ est un EDI basé sur PyQt et "
|
|
|
|
|
"l'outil d'édition Scintilla."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:44
|
|
|
|
|
msgid ""
|
|
|
|
|
"Pydb is a version of the standard Python debugger pdb, modified for use with "
|
|
|
|
|
"DDD (Data Display Debugger), a popular graphical debugger front end. Pydb "
|
|
|
|
|
"can be found at http://bashdb.sourceforge.net/pydb/ and DDD can be found at "
|
|
|
|
|
"https://www.gnu.org/software/ddd."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pydb est une version du débogueur standard Python pdb, modifié pour être "
|
|
|
|
|
"utilisé avec DDD (Data Display Debugger), un célèbre débogueur graphique. "
|
|
|
|
|
"Pydb est disponible sur http://bashdb.sourceforge.net/pydb/ et DDD est "
|
|
|
|
|
"disponible sur https://www.gnu.org/software/ddd."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:49
|
|
|
|
|
msgid ""
|
|
|
|
|
"There are a number of commercial Python IDEs that include graphical "
|
|
|
|
|
"debuggers. They include:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il existe de nombreux EDI Python propriétaires qui embarquent un débogueur "
|
|
|
|
|
"graphique. Notamment :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:52
|
|
|
|
|
msgid "Wing IDE (https://wingware.com/)"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Wing IDE (https://wingware.com/)"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:53
|
|
|
|
|
msgid "Komodo IDE (https://komodoide.com/)"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Komodo IDE (https://komodoide.com/)"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:54
|
|
|
|
|
msgid "PyCharm (https://www.jetbrains.com/pycharm/)"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "PyCharm (https://www.jetbrains.com/pycharm/)"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:58
|
|
|
|
|
msgid "Is there a tool to help find bugs or perform static analysis?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Existe-t'il des outils pour aider à trouver des bogues ou faire de l'analyse "
|
|
|
|
|
"statique de code ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:62
|
|
|
|
|
msgid ""
|
|
|
|
|
"PyChecker is a static analysis tool that finds bugs in Python source code "
|
|
|
|
|
"and warns about code complexity and style. You can get PyChecker from "
|
|
|
|
|
"http://pychecker.sourceforge.net/."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"PyChecker est un outil d'analyse statique qui trouve les bogues dans le code "
|
|
|
|
|
"source Python et émet des avertissements relatifs à la complexité et au "
|
|
|
|
|
"style du code. PyChecker est disponible sur http://pychecker.sourceforge."
|
|
|
|
|
"net/."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:66
|
|
|
|
|
msgid ""
|
|
|
|
|
"`Pylint <https://www.pylint.org/>`_ is another tool that checks if a module "
|
|
|
|
|
"satisfies a coding standard, and also makes it possible to write plug-ins to "
|
|
|
|
|
"add a custom feature. In addition to the bug checking that PyChecker "
|
|
|
|
|
"performs, Pylint offers some additional features such as checking line "
|
|
|
|
|
"length, whether variable names are well-formed according to your coding "
|
|
|
|
|
"standard, whether declared interfaces are fully implemented, and more. "
|
|
|
|
|
"https://docs.pylint.org/ provides a full list of Pylint's features."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pylint <https://www.pylint.org/>`_ est un autre outil qui vérifie si un "
|
|
|
|
|
"module satisfait aux normes de développement, et qui permet en plus d'écrire "
|
|
|
|
|
"des greffons pour ajouter des fonctionnalités personnalisées. En plus de la "
|
|
|
|
|
"vérification des bogues effectuée par PyChecker, Pylint effectue quelques "
|
|
|
|
|
"vérifications supplémentaires comme la longueur des lignes, les conventions "
|
|
|
|
|
"de nommage des variables, que les interfaces déclarées sont implémentées en "
|
|
|
|
|
"totalité, et plus encore. https://docs.pylint.org/ fournit la liste complète "
|
|
|
|
|
"des fonctionnalités de Pylint."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:76
|
|
|
|
|
msgid "How can I create a stand-alone binary from a Python script?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Comment créer un binaire autonome à partir d'un script Python ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:78
|
|
|
|
|
msgid ""
|
|
|
|
|
"You don't need the ability to compile Python to C code if all you want is a "
|
|
|
|
|
"stand-alone program that users can download and run without having to "
|
|
|
|
|
"install the Python distribution first. There are a number of tools that "
|
|
|
|
|
"determine the set of modules required by a program and bind these modules "
|
|
|
|
|
"together with a Python binary to produce a single executable."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pour créer un programme autonome, c'est-à-dire un programme que n'importe "
|
|
|
|
|
"qui peut télécharger et exécuter sans avoir à installer une distribution "
|
|
|
|
|
"Python au préalable, il n'est pas nécessaire de compiler du code Python en "
|
|
|
|
|
"code C. Il existe en effet plusieurs outils qui déterminent les modules "
|
|
|
|
|
"requis par un programme et lient ces modules avec un binaire Python pour "
|
|
|
|
|
"produire un seul exécutable."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:84
|
|
|
|
|
msgid ""
|
|
|
|
|
"One is to use the freeze tool, which is included in the Python source tree "
|
|
|
|
|
"as ``Tools/freeze``. It converts Python byte code to C arrays; a C compiler "
|
|
|
|
|
"you can embed all your modules into a new program, which is then linked with "
|
|
|
|
|
"the standard Python modules."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Un de ces outils est freeze, qui se trouve dans ``Tools/freeze`` de "
|
|
|
|
|
"l'arborescence des sources de Python. Il convertit le code intermédiaire "
|
|
|
|
|
"(*bytecode*) Python en tableaux C ; un compilateur C permet d'intégrer tous "
|
|
|
|
|
"vos modules dans un nouveau programme, qui est ensuite lié aux modules "
|
|
|
|
|
"standards Python."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:89
|
|
|
|
|
msgid ""
|
|
|
|
|
"It works by scanning your source recursively for import statements (in both "
|
|
|
|
|
"forms) and looking for the modules in the standard Python path as well as in "
|
|
|
|
|
"the source directory (for built-in modules). It then turns the bytecode for "
|
|
|
|
|
"modules written in Python into C code (array initializers that can be turned "
|
|
|
|
|
"into code objects using the marshal module) and creates a custom-made config "
|
|
|
|
|
"file that only contains those built-in modules which are actually used in "
|
|
|
|
|
"the program. It then compiles the generated C code and links it with the "
|
|
|
|
|
"rest of the Python interpreter to form a self-contained binary which acts "
|
|
|
|
|
"exactly like your script."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il fonctionne en cherchant de manière récursive les instructions d'import "
|
|
|
|
|
"(sous les deux formes) dans le code source et en recherchant ces modules "
|
|
|
|
|
"dans le chemin Python standard ainsi que dans le répertoire source (pour les "
|
|
|
|
|
"modules natifs). Il transforme ensuite le code intermédiaire des modules "
|
|
|
|
|
"écrits en Python en code C (des tableaux pré-remplis qui peuvent être "
|
|
|
|
|
"transformés en objets code à l'aide du module *marshal*) et crée un fichier "
|
|
|
|
|
"de configuration personnalisé qui contient uniquement les modules natifs qui "
|
|
|
|
|
"sont réellement utilisés dans le programme. Il compile ensuite le code C "
|
|
|
|
|
"généré et le lie au reste de l'interpréteur Python pour former un binaire "
|
|
|
|
|
"autonome qui fait exactement la même chose que le script."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:98
|
|
|
|
|
msgid ""
|
|
|
|
|
"Obviously, freeze requires a C compiler. There are several other utilities "
|
|
|
|
|
"which don't. One is Thomas Heller's py2exe (Windows only) at"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Bien évidemment, freeze nécessite un compilateur C. Il existe d'autres "
|
|
|
|
|
"outils qui peuvent s'en passer. Un de ceux-ci est py2exe de Thomas Heller "
|
|
|
|
|
"(pour Windows uniquement) disponible sur"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:101
|
|
|
|
|
msgid "http://www.py2exe.org/"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "http://www.py2exe.org/"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:103
|
|
|
|
|
msgid ""
|
|
|
|
|
"Another tool is Anthony Tuininga's `cx_Freeze <http://cx-freeze.sourceforge."
|
|
|
|
|
"net/>`_."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:107
|
|
|
|
|
msgid "Are there coding standards or a style guide for Python programs?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Existe-t'il des normes de développement ou un guide de style pour écrire des "
|
|
|
|
|
"programmes Python ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:109
|
|
|
|
|
msgid ""
|
|
|
|
|
"Yes. The coding style required for standard library modules is documented "
|
|
|
|
|
"as :pep:`8`."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Oui. Le style de développement que les modules de la bibliothèque standard "
|
|
|
|
|
"doivent obligatoirement respecter est documenté dans la :pep:`8`."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:114
|
|
|
|
|
msgid "My program is too slow. How do I speed it up?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Mon programme est trop lent. Comment l'accélérer ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:116
|
|
|
|
|
msgid ""
|
|
|
|
|
"That's a tough one, in general. There are many tricks to speed up Python "
|
|
|
|
|
"code; consider rewriting parts in C as a last resort."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:119
|
|
|
|
|
msgid ""
|
|
|
|
|
"In some cases it's possible to automatically translate Python to C or x86 "
|
|
|
|
|
"assembly language, meaning that you don't have to modify your code to gain "
|
|
|
|
|
"increased speed."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:125
|
|
|
|
|
msgid ""
|
|
|
|
|
"`Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_ can compile "
|
|
|
|
|
"a slightly modified version of Python code into a C extension, and can be "
|
|
|
|
|
"used on many different platforms."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:129
|
|
|
|
|
msgid ""
|
|
|
|
|
"`Psyco <http://psyco.sourceforge.net>`_ is a just-in-time compiler that "
|
|
|
|
|
"translates Python code into x86 assembly language. If you can use it, Psyco "
|
|
|
|
|
"can provide dramatic speedups for critical functions."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:133
|
|
|
|
|
msgid ""
|
|
|
|
|
"The rest of this answer will discuss various tricks for squeezing a bit more "
|
|
|
|
|
"speed out of Python code. *Never* apply any optimization tricks unless you "
|
|
|
|
|
"know you need them, after profiling has indicated that a particular function "
|
|
|
|
|
"is the heavily executed hot spot in the code. Optimizations almost always "
|
|
|
|
|
"make the code less clear, and you shouldn't pay the costs of reduced clarity "
|
|
|
|
|
"(increased development time, greater likelihood of bugs) unless the "
|
|
|
|
|
"resulting performance benefit is worth it."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:141
|
|
|
|
|
msgid ""
|
|
|
|
|
"There is a page on the wiki devoted to `performance tips <https://wiki."
|
|
|
|
|
"python.org/moin/PythonSpeed/PerformanceTips>`_."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:144
|
|
|
|
|
msgid ""
|
|
|
|
|
"Guido van Rossum has written up an anecdote related to optimization at "
|
|
|
|
|
"https://www.python.org/doc/essays/list2str."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:147
|
|
|
|
|
msgid ""
|
|
|
|
|
"One thing to notice is that function and (especially) method calls are "
|
|
|
|
|
"rather expensive; if you have designed a purely OO interface with lots of "
|
|
|
|
|
"tiny functions that don't do much more than get or set an instance variable "
|
|
|
|
|
"or call another method, you might consider using a more direct way such as "
|
|
|
|
|
"directly accessing instance variables. Also see the standard module :mod:"
|
|
|
|
|
"`profile` which makes it possible to find out where your program is spending "
|
|
|
|
|
"most of its time (if you have some patience -- the profiling itself can slow "
|
|
|
|
|
"your program down by an order of magnitude)."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:156
|
|
|
|
|
msgid ""
|
|
|
|
|
"Remember that many standard optimization heuristics you may know from other "
|
|
|
|
|
"programming experience may well apply to Python. For example it may be "
|
|
|
|
|
"faster to send output to output devices using larger writes rather than "
|
|
|
|
|
"smaller ones in order to reduce the overhead of kernel system calls. Thus "
|
|
|
|
|
"CGI scripts that write all output in \"one shot\" may be faster than those "
|
|
|
|
|
"that write lots of small pieces of output."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:163
|
|
|
|
|
msgid ""
|
|
|
|
|
"Also, be sure to use Python's core features where appropriate. For example, "
|
|
|
|
|
"slicing allows programs to chop up lists and other sequence objects in a "
|
|
|
|
|
"single tick of the interpreter's mainloop using highly optimized C "
|
|
|
|
|
"implementations. Thus to get the same effect as::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:172
|
|
|
|
|
msgid "it is much shorter and far faster to use ::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:176
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that the functionally-oriented built-in functions such as :func:`map`, :"
|
|
|
|
|
"func:`zip`, and friends can be a convenient accelerator for loops that "
|
|
|
|
|
"perform a single task. For example to pair the elements of two lists "
|
|
|
|
|
"together::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:184
|
|
|
|
|
msgid "or to compute a number of sines::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:189
|
|
|
|
|
msgid "The operation completes very quickly in such cases."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:191
|
|
|
|
|
msgid ""
|
|
|
|
|
"Other examples include the ``join()`` and ``split()`` :ref:`methods of "
|
|
|
|
|
"string objects <string-methods>`. For example if s1..s7 are large (10K+) "
|
|
|
|
|
"strings then ``\"\".join([s1,s2,s3,s4,s5,s6,s7])`` may be far faster than "
|
|
|
|
|
"the more obvious ``s1+s2+s3+s4+s5+s6+s7``, since the \"summation\" will "
|
|
|
|
|
"compute many subexpressions, whereas ``join()`` does all the copying in one "
|
|
|
|
|
"pass. For manipulating strings, use the ``replace()`` and the ``format()`` :"
|
|
|
|
|
"ref:`methods on string objects <string-methods>`. Use regular expressions "
|
|
|
|
|
"only when you're not dealing with constant string patterns. You may still "
|
|
|
|
|
"use :ref:`the old % operations <string-formatting>` ``string % tuple`` and "
|
|
|
|
|
"``string % dictionary``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:202
|
|
|
|
|
msgid ""
|
|
|
|
|
"Be sure to use the :meth:`list.sort` built-in method to do sorting, and see "
|
|
|
|
|
"the `sorting mini-HOWTO <https://wiki.python.org/moin/HowTo/Sorting>`_ for "
|
|
|
|
|
"examples of moderately advanced usage. :meth:`list.sort` beats other "
|
|
|
|
|
"techniques for sorting in all but the most extreme circumstances."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:207
|
|
|
|
|
msgid ""
|
|
|
|
|
"Another common trick is to \"push loops into functions or methods.\" For "
|
|
|
|
|
"example suppose you have a program that runs slowly and you use the profiler "
|
|
|
|
|
"to determine that a Python function ``ff()`` is being called lots of times. "
|
|
|
|
|
"If you notice that ``ff()``::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:216
|
|
|
|
|
msgid "tends to be called in loops like::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:220
|
|
|
|
|
msgid "or::"
|
|
|
|
|
msgstr "ou : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:226
|
|
|
|
|
msgid ""
|
|
|
|
|
"then you can often eliminate function call overhead by rewriting ``ff()`` "
|
|
|
|
|
"to::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:235
|
|
|
|
|
msgid "and rewrite the two examples to ``list = ffseq(oldlist)`` and to::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:240
|
|
|
|
|
msgid ""
|
|
|
|
|
"Single calls to ``ff(x)`` translate to ``ffseq([x])[0]`` with little "
|
|
|
|
|
"penalty. Of course this technique is not always appropriate and there are "
|
|
|
|
|
"other variants which you can figure out."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:244
|
|
|
|
|
msgid ""
|
|
|
|
|
"You can gain some performance by explicitly storing the results of a "
|
|
|
|
|
"function or method lookup into a local variable. A loop like::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:250
|
|
|
|
|
msgid ""
|
|
|
|
|
"resolves ``dict.get`` every iteration. If the method isn't going to change, "
|
|
|
|
|
"a slightly faster implementation is::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:257
|
|
|
|
|
msgid ""
|
|
|
|
|
"Default arguments can be used to determine values once, at compile time "
|
|
|
|
|
"instead of at run time. This can only be done for functions or objects "
|
|
|
|
|
"which will not be changed during program execution, such as replacing ::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:264
|
|
|
|
|
msgid "with ::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:269
|
|
|
|
|
msgid ""
|
|
|
|
|
"Because this trick uses default arguments for terms which should not be "
|
|
|
|
|
"changed, it should only be used when you are not concerned with presenting a "
|
|
|
|
|
"possibly confusing API to your users."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:275
|
|
|
|
|
msgid "Core Language"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Fondamentaux"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:278
|
|
|
|
|
msgid "Why am I getting an UnboundLocalError when the variable has a value?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pourquoi une UnboundLocalError est levée alors qu'une variable a une valeur ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:280
|
|
|
|
|
msgid ""
|
|
|
|
|
"It can be a surprise to get the UnboundLocalError in previously working code "
|
|
|
|
|
"when it is modified by adding an assignment statement somewhere in the body "
|
|
|
|
|
"of a function."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il est parfois surprenant d'obtenir une UnboundLocalError dans du code "
|
|
|
|
|
"jusqu'à présent correct, quand celui-ci est modifié en ajoutant une "
|
|
|
|
|
"instruction d'affectation quelque part dans le corps d'une fonction."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:284
|
|
|
|
|
msgid "This code:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Le code suivant :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:292
|
|
|
|
|
msgid "works, but this code:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "fonctionne, mais le suivant :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:299
|
|
|
|
|
msgid "results in an UnboundLocalError:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "lève une UnboundLocalError :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:306
|
|
|
|
|
msgid ""
|
|
|
|
|
"This is because when you make an assignment to a variable in a scope, that "
|
|
|
|
|
"variable becomes local to that scope and shadows any similarly named "
|
|
|
|
|
"variable in the outer scope. Since the last statement in foo assigns a new "
|
|
|
|
|
"value to ``x``, the compiler recognizes it as a local variable. "
|
|
|
|
|
"Consequently when the earlier ``print x`` attempts to print the "
|
|
|
|
|
"uninitialized local variable and an error results."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:313
|
|
|
|
|
msgid ""
|
|
|
|
|
"In the example above you can access the outer scope variable by declaring it "
|
|
|
|
|
"global:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Dans l'exemple ci-dessus, la variable du contexte appelant reste accessible "
|
|
|
|
|
"en la déclarant globale :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:324
|
|
|
|
|
msgid ""
|
|
|
|
|
"This explicit declaration is required in order to remind you that (unlike "
|
|
|
|
|
"the superficially analogous situation with class and instance variables) you "
|
|
|
|
|
"are actually modifying the value of the variable in the outer scope:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Cette déclaration explicite est obligatoire pour se rappeler que "
|
|
|
|
|
"(contrairement au cas à peu près similaire avec des variables de classe et "
|
|
|
|
|
"d'instance), c'est la valeur de la variable du contexte appelant qui est "
|
|
|
|
|
"modifiée :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:333
|
|
|
|
|
msgid "What are the rules for local and global variables in Python?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Quelles sont les règles pour les variables locales et globales en Python ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:335
|
|
|
|
|
msgid ""
|
|
|
|
|
"In Python, variables that are only referenced inside a function are "
|
|
|
|
|
"implicitly global. If a variable is assigned a value anywhere within the "
|
|
|
|
|
"function's body, it's assumed to be a local unless explicitly declared as "
|
|
|
|
|
"global."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"En Python, si une variable n'est pas modifiée dans une fonction mais "
|
|
|
|
|
"seulement lue, elle est implicitement considérée comme globale. Si une "
|
|
|
|
|
"valeur lui est affectée, elle est considérée locale (sauf si elle est "
|
|
|
|
|
"explicitement déclarée globale)."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:339
|
|
|
|
|
msgid ""
|
|
|
|
|
"Though a bit surprising at first, a moment's consideration explains this. "
|
|
|
|
|
"On one hand, requiring :keyword:`global` for assigned variables provides a "
|
|
|
|
|
"bar against unintended side-effects. On the other hand, if ``global`` was "
|
|
|
|
|
"required for all global references, you'd be using ``global`` all the time. "
|
|
|
|
|
"You'd have to declare as global every reference to a built-in function or to "
|
|
|
|
|
"a component of an imported module. This clutter would defeat the usefulness "
|
|
|
|
|
"of the ``global`` declaration for identifying side-effects."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Bien que surprenant au premier abord, ce choix s'explique facilement. D'une "
|
|
|
|
|
"part, exiger :keyword:`global` pour des variables affectées est une "
|
|
|
|
|
"protection contre des effets de bord inattendus. D'autre part, si ``global`` "
|
|
|
|
|
"était obligatoire pour toutes les références à des objets globaux, il "
|
|
|
|
|
"faudrait mettre ``global`` partout, car il faudrait dans ce cas déclarer "
|
|
|
|
|
"globale chaque référence à une fonction native ou à un composant d'un module "
|
|
|
|
|
"importé. Le codé serait alors truffé de déclarations ``global``, ce qui "
|
|
|
|
|
"nuirait à leur raison d'être : identifier les effets de bords."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:349
|
|
|
|
|
msgid ""
|
|
|
|
|
"Why do lambdas defined in a loop with different values all return the same "
|
|
|
|
|
"result?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pourquoi des expressions lambda définies dans une boucle avec des valeurs "
|
|
|
|
|
"différentes retournent-elles le même résultat ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:351
|
|
|
|
|
msgid ""
|
|
|
|
|
"Assume you use a for loop to define a few different lambdas (or even plain "
|
|
|
|
|
"functions), e.g.::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Supposons que l'on utilise une boucle itérative pour définir des expressions "
|
|
|
|
|
"lambda (voire même des fonctions) différentes, par exemple ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:358
|
|
|
|
|
msgid ""
|
|
|
|
|
"This gives you a list that contains 5 lambdas that calculate ``x**2``. You "
|
|
|
|
|
"might expect that, when called, they would return, respectively, ``0``, "
|
|
|
|
|
"``1``, ``4``, ``9``, and ``16``. However, when you actually try you will "
|
|
|
|
|
"see that they all return ``16``::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Le code précédent crée une liste de 5 expressions lambda qui calculent "
|
|
|
|
|
"chacune ``x**2``. En les exécutant, on pourrait s'attendre à obtenir ``0``, "
|
|
|
|
|
"``1``, ``4``, ``9`` et ``16``. Elles renvoient en réalité toutes ``16`` :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:368
|
|
|
|
|
msgid ""
|
|
|
|
|
"This happens because ``x`` is not local to the lambdas, but is defined in "
|
|
|
|
|
"the outer scope, and it is accessed when the lambda is called --- not when "
|
|
|
|
|
"it is defined. At the end of the loop, the value of ``x`` is ``4``, so all "
|
|
|
|
|
"the functions now return ``4**2``, i.e. ``16``. You can also verify this by "
|
|
|
|
|
"changing the value of ``x`` and see how the results of the lambdas change::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Ceci s'explique par le fait que ``x`` n'est pas une variable locale aux "
|
|
|
|
|
"expressions, mais est définie dans le contexte appelant. Elle est lue à "
|
|
|
|
|
"l'appel de l'expression lambda – et non au moment où cette expression est "
|
|
|
|
|
"définie. À la fin de la boucle, ``x`` vaut ``4``, donc toutes les fonctions "
|
|
|
|
|
"renvoient ``4*2``, i.e. ``16``. Ceci se vérifie également en changeant la "
|
|
|
|
|
"valeur de ``x`` et en constatant que les résultats sont modifiés :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:378
|
|
|
|
|
msgid ""
|
|
|
|
|
"In order to avoid this, you need to save the values in variables local to "
|
|
|
|
|
"the lambdas, so that they don't rely on the value of the global ``x``::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pour éviter ce phénomène, les valeurs doivent être stockées dans des "
|
|
|
|
|
"variables locales aux expressions lambda pour que celles-ci ne se basent "
|
|
|
|
|
"plus sur la variable globale ``x`` :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:385
|
|
|
|
|
msgid ""
|
|
|
|
|
"Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed "
|
|
|
|
|
"when the lambda is defined so that it has the same value that ``x`` had at "
|
|
|
|
|
"that point in the loop. This means that the value of ``n`` will be ``0`` in "
|
|
|
|
|
"the first lambda, ``1`` in the second, ``2`` in the third, and so on. "
|
|
|
|
|
"Therefore each lambda will now return the correct result::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Dans ce code, ``n=x`` crée une nouvelle variable ``n``, locale à "
|
|
|
|
|
"l'expression. Cette variable est évaluée quand l'expression est définie donc "
|
|
|
|
|
"``n`` a la même valeur que ``x`` à ce moment. La valeur de ``n`` est donc "
|
|
|
|
|
"``0`` dans la première lambda, ``1`` dans la deuxième, ``2`` dans la "
|
|
|
|
|
"troisième et ainsi de suite. Chaque expression lambda renvoie donc le "
|
|
|
|
|
"résultat correct :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:396
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that this behaviour is not peculiar to lambdas, but applies to regular "
|
|
|
|
|
"functions too."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Ce comportement n'est pas propre aux expressions lambda, mais s'applique "
|
|
|
|
|
"aussi aux fonctions normales."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:401
|
|
|
|
|
msgid "How do I share global variables across modules?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Comment partager des variables globales entre modules ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:403
|
|
|
|
|
msgid ""
|
|
|
|
|
"The canonical way to share information across modules within a single "
|
|
|
|
|
"program is to create a special module (often called config or cfg). Just "
|
|
|
|
|
"import the config module in all modules of your application; the module then "
|
|
|
|
|
"becomes available as a global name. Because there is only one instance of "
|
|
|
|
|
"each module, any changes made to the module object get reflected "
|
|
|
|
|
"everywhere. For example:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"La manière standard de partager des informations entre modules d'un même "
|
|
|
|
|
"programme est de créer un module spécial (souvent appelé *config* ou *cfg*) "
|
|
|
|
|
"et de l'importer dans tous les modules de l'application ; le module devient "
|
|
|
|
|
"accessible depuis l'espace de nommage global. Vu qu'il n'y a qu'une instance "
|
|
|
|
|
"de chaque module, tout changement dans l'instance est propagé partout. Par "
|
|
|
|
|
"exemple :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:409
|
|
|
|
|
msgid "config.py::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*config.py* ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:413
|
|
|
|
|
msgid "mod.py::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*mod.py* ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:418
|
|
|
|
|
msgid "main.py::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*main.py* ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:424
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that using a module is also the basis for implementing the Singleton "
|
|
|
|
|
"design pattern, for the same reason."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pour les mêmes raisons, l'utilisation d'un module est aussi à la base de "
|
|
|
|
|
"l'implémentation du patron de conception singleton."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:429
|
|
|
|
|
msgid "What are the \"best practices\" for using import in a module?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Quelles sont les « bonnes pratiques » pour utiliser import dans un module ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:431
|
|
|
|
|
msgid ""
|
|
|
|
|
"In general, don't use ``from modulename import *``. Doing so clutters the "
|
|
|
|
|
"importer's namespace, and makes it much harder for linters to detect "
|
|
|
|
|
"undefined names."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"De manière générale, il ne faut pas faire ``from modulename import *``. Ceci "
|
|
|
|
|
"encombre l'espace de nommage de l'importateur et rend la détection de noms "
|
|
|
|
|
"non-définis beaucoup plus ardue pour les analyseurs de code."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:435
|
|
|
|
|
msgid ""
|
|
|
|
|
"Import modules at the top of a file. Doing so makes it clear what other "
|
|
|
|
|
"modules your code requires and avoids questions of whether the module name "
|
|
|
|
|
"is in scope. Using one import per line makes it easy to add and delete "
|
|
|
|
|
"module imports, but using multiple imports per line uses less screen space."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Les modules doivent être importés au début d'un fichier. Ceci permet "
|
|
|
|
|
"d'afficher clairement de quels modules le code à besoin et évite de se "
|
|
|
|
|
"demander si le module est dans le contexte. Faire un seul `import` par ligne "
|
|
|
|
|
"rend l'ajout et la suppression d'une importation de module plus aisé, mais "
|
|
|
|
|
"importer plusieurs modules sur une même ligne prend moins d'espace."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:440
|
|
|
|
|
msgid "It's good practice if you import modules in the following order:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Il est recommandé d'importer les modules dans l'ordre suivant :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:442
|
|
|
|
|
msgid "standard library modules -- e.g. ``sys``, ``os``, ``getopt``, ``re``"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"les modules de la bibliothèque standard — e.g. ``sys``, ``os``, ``getopt``, "
|
|
|
|
|
"``re``"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:443
|
|
|
|
|
msgid ""
|
|
|
|
|
"third-party library modules (anything installed in Python's site-packages "
|
|
|
|
|
"directory) -- e.g. mx.DateTime, ZODB, PIL.Image, etc."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"les modules externes (tout ce qui est installé dans le dossier *site-"
|
|
|
|
|
"packages* de Python) — e.g. *mx.DateTime, ZODB, PIL.Image*, etc."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:445
|
|
|
|
|
msgid "locally-developed modules"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "les modules développés en local"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:447
|
|
|
|
|
msgid ""
|
|
|
|
|
"Only use explicit relative package imports. If you're writing code that's "
|
|
|
|
|
"in the ``package.sub.m1`` module and want to import ``package.sub.m2``, do "
|
|
|
|
|
"not just write ``import m2``, even though it's legal. Write ``from package."
|
|
|
|
|
"sub import m2`` or ``from . import m2`` instead."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:452
|
|
|
|
|
msgid ""
|
|
|
|
|
"It is sometimes necessary to move imports to a function or class to avoid "
|
|
|
|
|
"problems with circular imports. Gordon McMillan says:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il est parfois nécessaire de déplacer des importations dans une fonction ou "
|
|
|
|
|
"une classe pour éviter les problèmes d'importations circulaires. Comme le "
|
|
|
|
|
"souligne Gordon McMillan :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:455
|
|
|
|
|
msgid ""
|
|
|
|
|
"Circular imports are fine where both modules use the \"import <module>\" "
|
|
|
|
|
"form of import. They fail when the 2nd module wants to grab a name out of "
|
|
|
|
|
"the first (\"from module import name\") and the import is at the top level. "
|
|
|
|
|
"That's because names in the 1st are not yet available, because the first "
|
|
|
|
|
"module is busy importing the 2nd."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il n'y a aucun souci à faire des importations circulaires tant que les deux "
|
|
|
|
|
"modules utilisent la forme \"import <module>\" . Ça ne pose problème que si "
|
|
|
|
|
"le second module cherche à récupérer un nom du premier module (*\"from "
|
|
|
|
|
"module import name\"*) et que l'importation est dans l'espace de nommage du "
|
|
|
|
|
"fichier. Les noms du premier module ne sont en effet pas encore disponibles "
|
|
|
|
|
"car le premier module est occupé à importer le second."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:461
|
|
|
|
|
msgid ""
|
|
|
|
|
"In this case, if the second module is only used in one function, then the "
|
|
|
|
|
"import can easily be moved into that function. By the time the import is "
|
|
|
|
|
"called, the first module will have finished initializing, and the second "
|
|
|
|
|
"module can do its import."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Dans ce cas, si le second module n'est utilisé que dans une fonction, "
|
|
|
|
|
"l'importation peut facilement être déplacée dans cette fonction. Au moment "
|
|
|
|
|
"où l'importation sera appelée, le premier module aura fini de s'initialiser "
|
|
|
|
|
"et le second pourra faire son importation."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:466
|
|
|
|
|
msgid ""
|
|
|
|
|
"It may also be necessary to move imports out of the top level of code if "
|
|
|
|
|
"some of the modules are platform-specific. In that case, it may not even be "
|
|
|
|
|
"possible to import all of the modules at the top of the file. In this case, "
|
|
|
|
|
"importing the correct modules in the corresponding platform-specific code is "
|
|
|
|
|
"a good option."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il peut parfois être nécessaire de déplacer des importations de modules hors "
|
|
|
|
|
"de l'espace de plus haut niveau du code si certains de ces modules dépendent "
|
|
|
|
|
"de la machine utilisée. Dans ce cas de figure, il est parfois impossible "
|
|
|
|
|
"d'importer tous les modules au début du fichier. Dans ce cas, il est "
|
|
|
|
|
"recommandé d'importer les modules adéquats dans le code spécifique à la "
|
|
|
|
|
"machine."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:471
|
|
|
|
|
msgid ""
|
|
|
|
|
"Only move imports into a local scope, such as inside a function definition, "
|
|
|
|
|
"if it's necessary to solve a problem such as avoiding a circular import or "
|
|
|
|
|
"are trying to reduce the initialization time of a module. This technique is "
|
|
|
|
|
"especially helpful if many of the imports are unnecessary depending on how "
|
|
|
|
|
"the program executes. You may also want to move imports into a function if "
|
|
|
|
|
"the modules are only ever used in that function. Note that loading a module "
|
|
|
|
|
"the first time may be expensive because of the one time initialization of "
|
|
|
|
|
"the module, but loading a module multiple times is virtually free, costing "
|
|
|
|
|
"only a couple of dictionary lookups. Even if the module name has gone out "
|
|
|
|
|
"of scope, the module is probably available in :data:`sys.modules`."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Les imports ne devraient être déplacés dans un espace local, comme dans la "
|
|
|
|
|
"définition d'une fonction, que si cela est nécessaire pour résoudre un "
|
|
|
|
|
"problème comme éviter des dépendances circulaires ou réduire le temps "
|
|
|
|
|
"d'initialisation d'un module. Cette technique est particulièrement utile si "
|
|
|
|
|
"la majorité des imports est superflue selon le flux d'exécution du "
|
|
|
|
|
"programme. Il est également pertinent de déplacer des importations dans une "
|
|
|
|
|
"fonction si le module n'est utilisé qu'au sein de cette fonction. Le premier "
|
|
|
|
|
"chargement d'un module peut être coûteux à cause du coût fixe "
|
|
|
|
|
"d'initialisation d'un module, mais charger un module plusieurs fois est "
|
|
|
|
|
"quasiment gratuit, cela ne coûte que quelques indirections dans un "
|
|
|
|
|
"dictionnaire. Même si le nom du module est sorti du contexte courant, le "
|
|
|
|
|
"module est probablement disponible dans :data:`sys.modules`."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:484
|
|
|
|
|
msgid "Why are default values shared between objects?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Pourquoi les arguments par défaut sont-ils partagés entre les objets ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:486
|
|
|
|
|
msgid ""
|
|
|
|
|
"This type of bug commonly bites neophyte programmers. Consider this "
|
|
|
|
|
"function::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"C'est un problème que rencontrent souvent les programmeurs débutants. "
|
|
|
|
|
"Examinons la fonction suivante ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:493
|
|
|
|
|
msgid ""
|
|
|
|
|
"The first time you call this function, ``mydict`` contains a single item. "
|
|
|
|
|
"The second time, ``mydict`` contains two items because when ``foo()`` begins "
|
|
|
|
|
"executing, ``mydict`` starts out with an item already in it."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Au premier appel de cette fonction, ``mydict`` ne contient qu'un seul "
|
|
|
|
|
"élément. Au second appel, ``mydict`` contient deux éléments car quand "
|
|
|
|
|
"``foo()`` commence son exécution, ``mydict`` contient déjà un élément."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:497
|
|
|
|
|
msgid ""
|
|
|
|
|
"It is often expected that a function call creates new objects for default "
|
|
|
|
|
"values. This is not what happens. Default values are created exactly once, "
|
|
|
|
|
"when the function is defined. If that object is changed, like the "
|
|
|
|
|
"dictionary in this example, subsequent calls to the function will refer to "
|
|
|
|
|
"this changed object."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"On est souvent amené à croire qu'un appel de fonction créé des nouveau "
|
|
|
|
|
"objets pour les valeurs par défaut. Ce n'est pas le cas. Les valeurs par "
|
|
|
|
|
"défaut ne sont créées qu'une et une seule fois, au moment où la fonction est "
|
|
|
|
|
"définie. Si l'objet est modifié, comme le dictionnaire dans cet exemple, les "
|
|
|
|
|
"appels suivants à cette fonction font référence à l'objet ainsi modifié."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:502
|
|
|
|
|
msgid ""
|
|
|
|
|
"By definition, immutable objects such as numbers, strings, tuples, and "
|
|
|
|
|
"``None``, are safe from change. Changes to mutable objects such as "
|
|
|
|
|
"dictionaries, lists, and class instances can lead to confusion."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Par définition, les objets immuables comme les nombres, les chaînes de "
|
|
|
|
|
"caractères, les n-uplets et ``None`` ne sont pas modifiés. Les changements "
|
|
|
|
|
"sur des objets muables comme les dictionnaires, les listes et les instances "
|
|
|
|
|
"de classe peuvent porter à confusion."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:506
|
|
|
|
|
msgid ""
|
|
|
|
|
"Because of this feature, it is good programming practice to not use mutable "
|
|
|
|
|
"objects as default values. Instead, use ``None`` as the default value and "
|
|
|
|
|
"inside the function, check if the parameter is ``None`` and create a new "
|
|
|
|
|
"list/dictionary/whatever if it is. For example, don't write::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"En raison de cette fonctionnalité, il vaut mieux ne pas utiliser d'objets "
|
|
|
|
|
"muables comme valeurs par défaut. Il vaut mieux utiliser ``None`` comme "
|
|
|
|
|
"valeur par défaut et, à l'intérieur de la fonction, vérifier si le paramètre "
|
|
|
|
|
"est à ``None`` et créer une nouvelle liste, dictionnaire ou autre, le cas "
|
|
|
|
|
"échéant. Par exemple, il ne faut pas écrire ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:514
|
|
|
|
|
msgid "but::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "mais plutôt ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:520
|
|
|
|
|
msgid ""
|
|
|
|
|
"This feature can be useful. When you have a function that's time-consuming "
|
|
|
|
|
"to compute, a common technique is to cache the parameters and the resulting "
|
|
|
|
|
"value of each call to the function, and return the cached value if the same "
|
|
|
|
|
"value is requested again. This is called \"memoizing\", and can be "
|
|
|
|
|
"implemented like this::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Cette fonctionnalité a une utilité. Il est courant de mettre en cache les "
|
|
|
|
|
"paramètres et la valeur de retour de chacun des appels d'une fonction "
|
|
|
|
|
"coûteuse à exécuter, et de renvoyer la valeur stockée en cache si le même "
|
|
|
|
|
"appel est ré-effectué. C'est la technique dite de « mémoïsation », qui "
|
|
|
|
|
"s'implémente de la manière suivante ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:535
|
|
|
|
|
msgid ""
|
|
|
|
|
"You could use a global variable containing a dictionary instead of the "
|
|
|
|
|
"default value; it's a matter of taste."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il est possible d'utiliser une variable globale contenant un dictionnaire à "
|
|
|
|
|
"la place de la valeur par défaut ; ce n'est qu'une question de goût."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:540
|
|
|
|
|
msgid ""
|
|
|
|
|
"How can I pass optional or keyword parameters from one function to another?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Comment passer des paramètres optionnels ou nommés d'une fonction à l'autre ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:542
|
|
|
|
|
msgid ""
|
|
|
|
|
"Collect the arguments using the ``*`` and ``**`` specifiers in the "
|
|
|
|
|
"function's parameter list; this gives you the positional arguments as a "
|
|
|
|
|
"tuple and the keyword arguments as a dictionary. You can then pass these "
|
|
|
|
|
"arguments when calling another function by using ``*`` and ``**``::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il faut récupérer les arguments en utilisant les sélecteurs ``*`` et ``**`` "
|
|
|
|
|
"dans la liste des paramètres de la fonction ; ceci donne les arguments "
|
|
|
|
|
"positionnels sous la forme d'un n-uplet et les arguments nommés sous forme "
|
|
|
|
|
"de dictionnaire. Ces arguments peuvent être passés à une autre fonction en "
|
|
|
|
|
"utilisant ``*`` et ``**`` ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:553
|
|
|
|
|
msgid ""
|
|
|
|
|
"In the unlikely case that you care about Python versions older than 2.0, "
|
|
|
|
|
"use :func:`apply`::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:570
|
|
|
|
|
msgid "What is the difference between arguments and parameters?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Quelle est la différence entre les arguments et les paramètres ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:572
|
|
|
|
|
msgid ""
|
|
|
|
|
":term:`Parameters <parameter>` are defined by the names that appear in a "
|
|
|
|
|
"function definition, whereas :term:`arguments <argument>` are the values "
|
|
|
|
|
"actually passed to a function when calling it. Parameters define what types "
|
|
|
|
|
"of arguments a function can accept. For example, given the function "
|
|
|
|
|
"definition::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Les :term:`paramètres <parameter>` sont les noms qui apparaissent dans une "
|
|
|
|
|
"définition de fonction, alors que les :term:`arguments <argument>` sont les "
|
|
|
|
|
"valeurs qui sont réellement passées à une fonction lors de l'appel de celle-"
|
|
|
|
|
"ci. Les paramètres définissent les types des arguments qu'une fonction "
|
|
|
|
|
"accepte. Ainsi, avec la définition de fonction suivante ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:580
|
|
|
|
|
msgid ""
|
|
|
|
|
"*foo*, *bar* and *kwargs* are parameters of ``func``. However, when calling "
|
|
|
|
|
"``func``, for example::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"*foo*, *bar* et *kwargs* sont des paramètres de ``func``. Mais à l'appel de "
|
|
|
|
|
"``func`` avec, par exemple ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:585
|
|
|
|
|
msgid "the values ``42``, ``314``, and ``somevar`` are arguments."
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "les valeurs ``42``, ``314``, et ``somevar`` sont des arguments."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:589
|
|
|
|
|
msgid "Why did changing list 'y' also change list 'x'?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Pourquoi modifier la liste 'y' modifie aussi la liste 'x' ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:591
|
|
|
|
|
msgid "If you wrote code like::"
|
|
|
|
|
msgstr "Si vous avez écrit du code comme : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:601
|
|
|
|
|
msgid ""
|
|
|
|
|
"you might be wondering why appending an element to ``y`` changed ``x`` too."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"vous vous demandez peut-être pourquoi l'ajout d'un élément à ``y`` a aussi "
|
|
|
|
|
"changé ``x``."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:603
|
|
|
|
|
msgid "There are two factors that produce this result:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Il y a deux raisons qui conduisent à ce comportement :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:605
|
|
|
|
|
msgid ""
|
|
|
|
|
"Variables are simply names that refer to objects. Doing ``y = x`` doesn't "
|
|
|
|
|
"create a copy of the list -- it creates a new variable ``y`` that refers to "
|
|
|
|
|
"the same object ``x`` refers to. This means that there is only one object "
|
|
|
|
|
"(the list), and both ``x`` and ``y`` refer to it."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Les variables ne sont que des noms qui font référence à des objets. La ligne "
|
|
|
|
|
"``y = x`` ne crée pas une copie de la liste — elle crée une nouvelle "
|
|
|
|
|
"variable ``y`` qui pointe sur le même objet que ``x``. Ceci signifie qu'il "
|
|
|
|
|
"n'existe qu'un seul objet (la liste) auquel ``x`` et ``y`` font référence."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:609
|
|
|
|
|
msgid ""
|
|
|
|
|
"Lists are :term:`mutable`, which means that you can change their content."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Les listes sont des :term:`muable`, ce qui signifie que leur contenu peut "
|
|
|
|
|
"être modifié."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:611
|
|
|
|
|
msgid ""
|
|
|
|
|
"After the call to :meth:`~list.append`, the content of the mutable object "
|
|
|
|
|
"has changed from ``[]`` to ``[10]``. Since both the variables refer to the "
|
|
|
|
|
"same object, using either name accesses the modified value ``[10]``."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Après l'appel de :meth:`~list.append`, le contenu de l'objet muable est "
|
|
|
|
|
"passé de ``[]`` à ``[10]``. Vu que les deux variables font référence au même "
|
|
|
|
|
"objet, il est possible d'accéder à la valeur modifiée ``[10]`` avec chacun "
|
|
|
|
|
"des noms."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:615
|
|
|
|
|
msgid "If we instead assign an immutable object to ``x``::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Si au contraire, on affecte un objet immuable à ``x`` ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:625
|
|
|
|
|
msgid ""
|
|
|
|
|
"we can see that in this case ``x`` and ``y`` are not equal anymore. This is "
|
|
|
|
|
"because integers are :term:`immutable`, and when we do ``x = x + 1`` we are "
|
|
|
|
|
"not mutating the int ``5`` by incrementing its value; instead, we are "
|
|
|
|
|
"creating a new object (the int ``6``) and assigning it to ``x`` (that is, "
|
|
|
|
|
"changing which object ``x`` refers to). After this assignment we have two "
|
|
|
|
|
"objects (the ints ``6`` and ``5``) and two variables that refer to them "
|
|
|
|
|
"(``x`` now refers to ``6`` but ``y`` still refers to ``5``)."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"on observe que ``x`` et ``y`` ne sont ici plus égales. Les entiers sont des "
|
|
|
|
|
"immuables (:term:`immutable`), et ``x = x + 1`` ne change pas l'entier ``5`` "
|
|
|
|
|
"en incrémentant sa valeur. Au contraire, un nouvel objet est créé (l'entier "
|
|
|
|
|
"``6``) et affecté à ``x`` (c'est à dire qu'on change l'objet auquel fait "
|
|
|
|
|
"référence ``x``). Après cette affectation on a deux objets (les entiers "
|
|
|
|
|
"``6`` et ``5``) et deux variables qui font référence à ces deux objets "
|
|
|
|
|
"(``x`` fait désormais référence à ``6`` mais ``y`` fait toujours référence à "
|
|
|
|
|
"``5``)."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:633
|
|
|
|
|
msgid ""
|
|
|
|
|
"Some operations (for example ``y.append(10)`` and ``y.sort()``) mutate the "
|
|
|
|
|
"object, whereas superficially similar operations (for example ``y = y + "
|
|
|
|
|
"[10]`` and ``sorted(y)``) create a new object. In general in Python (and in "
|
|
|
|
|
"all cases in the standard library) a method that mutates an object will "
|
|
|
|
|
"return ``None`` to help avoid getting the two types of operations confused. "
|
|
|
|
|
"So if you mistakenly write ``y.sort()`` thinking it will give you a sorted "
|
|
|
|
|
"copy of ``y``, you'll instead end up with ``None``, which will likely cause "
|
|
|
|
|
"your program to generate an easily diagnosed error."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Certaines opérations (par exemple, ``y.append(10)`` et ``y.sort()``) "
|
|
|
|
|
"modifient l'objet, alors que des opérations identiques en apparence (par "
|
|
|
|
|
"exemple ``y = y + [10]`` et ``sorted(y)``) créent un nouvel objet. En "
|
|
|
|
|
"général, en Python, une méthode qui modifie un objet renvoie ``None`` (c'est "
|
|
|
|
|
"même systématique dans la bibliothèque standard) pour éviter la confusion "
|
|
|
|
|
"entre les deux opérations. Donc écrire par erreur ``y.sort()`` en pensant "
|
|
|
|
|
"obtenir une copie triée de ``y`` donne ``None``, ce qui conduit très souvent "
|
|
|
|
|
"le programme à générer une erreur facile à diagnostiquer."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:642
|
|
|
|
|
msgid ""
|
|
|
|
|
"However, there is one class of operations where the same operation sometimes "
|
|
|
|
|
"has different behaviors with different types: the augmented assignment "
|
|
|
|
|
"operators. For example, ``+=`` mutates lists but not tuples or ints "
|
|
|
|
|
"(``a_list += [1, 2, 3]`` is equivalent to ``a_list.extend([1, 2, 3])`` and "
|
|
|
|
|
"mutates ``a_list``, whereas ``some_tuple += (1, 2, 3)`` and ``some_int += "
|
|
|
|
|
"1`` create new objects)."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il existe cependant une classe d'opérations qui se comporte différemment "
|
|
|
|
|
"selon le type : les opérateurs d'affectation incrémentaux. Par exemple, ``"
|
|
|
|
|
"+=`` modifie les listes mais pas les n-uplets ni les entiers (``a_list += "
|
|
|
|
|
"[1, 2, 3]`` équivaut à ``a_list.extend([1, 2, 3])`` et modifie ``a_list``, "
|
|
|
|
|
"alors que ``some_tuple += (1, 2, 3)`` et ``some_int += 1`` créent de "
|
|
|
|
|
"nouveaux objets)."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:649
|
|
|
|
|
msgid "In other words:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "En d'autres termes :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:651
|
|
|
|
|
msgid ""
|
|
|
|
|
"If we have a mutable object (:class:`list`, :class:`dict`, :class:`set`, "
|
|
|
|
|
"etc.), we can use some specific operations to mutate it and all the "
|
|
|
|
|
"variables that refer to it will see the change."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Il est possible d'appliquer des opérations qui modifient un objet muable (:"
|
|
|
|
|
"class:`list`, :class:`dict`, :class:`set`, etc.) et toutes les variables qui "
|
|
|
|
|
"y font référence verront le changement."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:654
|
|
|
|
|
msgid ""
|
|
|
|
|
"If we have an immutable object (:class:`str`, :class:`int`, :class:`tuple`, "
|
|
|
|
|
"etc.), all the variables that refer to it will always see the same value, "
|
|
|
|
|
"but operations that transform that value into a new value always return a "
|
|
|
|
|
"new object."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Toutes les variables qui font référence à un objet immuable (:class:`str`, :"
|
|
|
|
|
"class:`int`, :class:`tuple`, etc.) renvoient la même valeur, mais les "
|
|
|
|
|
"opérations qui transforment cette valeur en une nouvelle valeur renvoient "
|
|
|
|
|
"toujours un nouvel objet."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:659
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you want to know if two variables refer to the same object or not, you "
|
|
|
|
|
"can use the :keyword:`is` operator, or the built-in function :func:`id`."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"L'opérateur :keyword:`is` ou la fonction native :func:`id` permettent de "
|
|
|
|
|
"savoir si deux variables font référence au même objet."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:664
|
|
|
|
|
msgid "How do I write a function with output parameters (call by reference)?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Comment écrire une fonction qui modifie ses paramètres ? (passage par "
|
|
|
|
|
"référence)"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:666
|
|
|
|
|
msgid ""
|
|
|
|
|
"Remember that arguments are passed by assignment in Python. Since "
|
|
|
|
|
"assignment just creates references to objects, there's no alias between an "
|
|
|
|
|
"argument name in the caller and callee, and so no call-by-reference per se. "
|
|
|
|
|
"You can achieve the desired effect in a number of ways."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"En Python, les arguments sont passés comme des affectations de variables. Vu "
|
|
|
|
|
"qu'une affectation crée des références à des objets, il n'y pas de lien "
|
|
|
|
|
"entre un argument dans l'appel de la fonction et sa définition, et donc pas "
|
|
|
|
|
"de passage par référence en soi. Il y a cependant plusieurs façon d'en "
|
|
|
|
|
"émuler un."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:671
|
|
|
|
|
msgid "By returning a tuple of the results::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "En renvoyant un n-uplet de résultats ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:682
|
|
|
|
|
msgid "This is almost always the clearest solution."
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "C'est presque toujours la meilleure solution."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:684
|
|
|
|
|
msgid ""
|
|
|
|
|
"By using global variables. This isn't thread-safe, and is not recommended."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"En utilisant des variables globales. Ce qui n'est pas *thread-safe*, et "
|
|
|
|
|
"n'est donc pas recommandé."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:686
|
|
|
|
|
msgid "By passing a mutable (changeable in-place) object::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "En passant un objet muable (modifiable sur place) ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:696
|
|
|
|
|
msgid "By passing in a dictionary that gets mutated::"
|
|
|
|
|
msgstr "En passant un dictionnaire, qui sera modifié : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:706
|
|
|
|
|
msgid "Or bundle up values in a class instance::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Ou regrouper les valeurs dans une instance de classe ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:722
|
|
|
|
|
msgid "There's almost never a good reason to get this complicated."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Il n'y a pratiquement jamais de bonne raison de faire quelque chose d'aussi "
|
|
|
|
|
"compliqué."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:724
|
|
|
|
|
msgid "Your best choice is to return a tuple containing the multiple results."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Votre meilleure option est de renvoyer un *tuple* contenant les multiples "
|
|
|
|
|
"résultats."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:728
|
|
|
|
|
msgid "How do you make a higher order function in Python?"
|
|
|
|
|
msgstr "Comment construire une fonction d'ordre supérieur en Python ?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:730
|
|
|
|
|
msgid ""
|
|
|
|
|
"You have two choices: you can use nested scopes or you can use callable "
|
|
|
|
|
"objects. For example, suppose you wanted to define ``linear(a,b)`` which "
|
|
|
|
|
"returns a function ``f(x)`` that computes the value ``a*x+b``. Using nested "
|
|
|
|
|
"scopes::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Vous avez deux choix : vous pouvez utiliser les portées imbriquées ou vous "
|
|
|
|
|
"pouvez utiliser des objets appelables. Par exemple, supposons que vous "
|
2017-10-23 20:33:03 +00:00
|
|
|
|
"vouliez définir ``linear(a, b)`` qui renvoie une fonction ``f(x)`` qui "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"calcule la valeur ``a*x+b``. En utilisant les portées imbriquées : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:739
|
|
|
|
|
msgid "Or using a callable object::"
|
|
|
|
|
msgstr "Ou en utilisant un objet appelable : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:749
|
|
|
|
|
msgid "In both cases, ::"
|
|
|
|
|
msgstr "dans les deux cas, ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:753
|
|
|
|
|
msgid "gives a callable object where ``taxes(10e6) == 0.3 * 10e6 + 2``."
|
|
|
|
|
msgstr "donne un objet appelable où ``taxes(10e6) == 0.3 * 10e6 + 2``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:755
|
|
|
|
|
msgid ""
|
|
|
|
|
"The callable object approach has the disadvantage that it is a bit slower "
|
|
|
|
|
"and results in slightly longer code. However, note that a collection of "
|
|
|
|
|
"callables can share their signature via inheritance::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"L'approche par objet appelable a le désavantage d'être légèrement plus lente "
|
|
|
|
|
"et de produire un code légèrement plus long. Cependant, il faut noter qu'une "
|
|
|
|
|
"collection d'objet appelables peuvent partager leur signatures par "
|
|
|
|
|
"héritage : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:764
|
|
|
|
|
msgid "Object can encapsulate state for several methods::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Les objets peuvent encapsuler un état pour plusieurs méthodes ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:782
|
|
|
|
|
msgid ""
|
|
|
|
|
"Here ``inc()``, ``dec()`` and ``reset()`` act like functions which share the "
|
|
|
|
|
"same counting variable."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Ici ``inc()``, ``dec()`` et ``reset()`` agissent comme des fonctions "
|
|
|
|
|
"partageant une même variable compteur."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:787
|
|
|
|
|
msgid "How do I copy an object in Python?"
|
|
|
|
|
msgstr "Comment copier un objet en Python?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:789
|
|
|
|
|
msgid ""
|
|
|
|
|
"In general, try :func:`copy.copy` or :func:`copy.deepcopy` for the general "
|
|
|
|
|
"case. Not all objects can be copied, but most can."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"En général, essayez :func:`copy.copy` ou :func:`copy.deepcopy` pour le cas "
|
|
|
|
|
"général. Tout les objets ne peuvent pas être copiés, mais la plupart le "
|
|
|
|
|
"peuvent."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:792
|
|
|
|
|
msgid ""
|
|
|
|
|
"Some objects can be copied more easily. Dictionaries have a :meth:`~dict."
|
|
|
|
|
"copy` method::"
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Certains objets peuvent être copiés plus facilement. Les Dictionnaires ont "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"une méthode :meth:`~dict.copy` ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:797
|
|
|
|
|
msgid "Sequences can be copied by slicing::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Les séquences peuvent être copiées via la syntaxe des tranches ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:803
|
|
|
|
|
msgid "How can I find the methods or attributes of an object?"
|
|
|
|
|
msgstr "Comment puis-je trouver les méthodes ou les attribues d'un objet?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:805
|
|
|
|
|
msgid ""
|
|
|
|
|
"For an instance x of a user-defined class, ``dir(x)`` returns an "
|
|
|
|
|
"alphabetized list of the names containing the instance attributes and "
|
|
|
|
|
"methods and attributes defined by its class."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Pour une instance x d'une classe définie par un utilisateur, ``dir(x)`` "
|
|
|
|
|
"renvoie une liste alphabétique des noms contenants les attributs de "
|
|
|
|
|
"l'instance, et les attributs et méthodes définies par sa classe."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:811
|
|
|
|
|
msgid "How can my code discover the name of an object?"
|
|
|
|
|
msgstr "Comment mon code peut il découvrir le nom d'un objet?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:813
|
|
|
|
|
msgid ""
|
|
|
|
|
"Generally speaking, it can't, because objects don't really have names. "
|
|
|
|
|
"Essentially, assignment always binds a name to a value; The same is true of "
|
|
|
|
|
"``def`` and ``class`` statements, but in that case the value is a callable. "
|
|
|
|
|
"Consider the following code::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"De façon générale, il ne peut pas, par ce que les objets n'ont pas "
|
|
|
|
|
"réellement de noms. Essentiellement, l'assignation attache un nom à une "
|
|
|
|
|
"valeur; C'est vrai aussi pour les instructions ``def`` et ``class``, à la "
|
|
|
|
|
"différence que dans ce cas la valeur est appelable. Par exemple, dans le "
|
|
|
|
|
"code suivant : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:829
|
|
|
|
|
msgid ""
|
|
|
|
|
"Arguably the class has a name: even though it is bound to two names and "
|
|
|
|
|
"invoked through the name B the created instance is still reported as an "
|
|
|
|
|
"instance of class A. However, it is impossible to say whether the "
|
|
|
|
|
"instance's name is a or b, since both names are bound to the same value."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Le fait que la classe ait un nom est discutable, bien qu'elles soit liée à "
|
|
|
|
|
"deux noms, et qu'elle soit appelée via le nom B, l'instance crée déclare "
|
|
|
|
|
"tout de même être une instance de la classe A. De même Il est impossible de "
|
|
|
|
|
"dire si le nom de l'instance est a ou b, les deux noms sont attachés à la "
|
|
|
|
|
"même valeur."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:834
|
|
|
|
|
msgid ""
|
|
|
|
|
"Generally speaking it should not be necessary for your code to \"know the "
|
|
|
|
|
"names\" of particular values. Unless you are deliberately writing "
|
|
|
|
|
"introspective programs, this is usually an indication that a change of "
|
|
|
|
|
"approach might be beneficial."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"De façon général, il ne devrait pas être nécessaire pour votre application "
|
|
|
|
|
"de \"connaître le nom\" d'une valeur particulière. À moins que vous soyez "
|
|
|
|
|
"délibérément en train d'écrire un programme introspectif, c'est souvent une "
|
|
|
|
|
"indication qu'un changement d'approche pourrait être bénéfique."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:839
|
|
|
|
|
msgid ""
|
|
|
|
|
"In comp.lang.python, Fredrik Lundh once gave an excellent analogy in answer "
|
|
|
|
|
"to this question:"
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Sur *comp.lang.python*, Fredrik Lundh a donné un jour une excellente "
|
|
|
|
|
"analogie pour répondre à cette question:"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:842
|
|
|
|
|
msgid ""
|
|
|
|
|
"The same way as you get the name of that cat you found on your porch: the "
|
|
|
|
|
"cat (object) itself cannot tell you its name, and it doesn't really care -- "
|
|
|
|
|
"so the only way to find out what it's called is to ask all your neighbours "
|
|
|
|
|
"(namespaces) if it's their cat (object)..."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"C'est pareil que trouver le nom du chat qui traîne devant votre porte : Le "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"chat (objet) ne peux pas vous dire lui même son nom, et il s'en moque un peu "
|
|
|
|
|
"-- alors le meilleur moyen de savoir comment il s'appelle est de demander à "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"tous vos voisins (espaces de noms) si c'est leur chat (objet)…."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:847
|
|
|
|
|
msgid ""
|
|
|
|
|
"....and don't be surprised if you'll find that it's known by many names, or "
|
|
|
|
|
"no name at all!"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"…et ne soyez pas surpris si vous découvrez qu'il est connus sous plusieurs "
|
|
|
|
|
"noms différents, ou pas de nom du tout!"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:852
|
|
|
|
|
msgid "What's up with the comma operator's precedence?"
|
|
|
|
|
msgstr "Qu'en est-il de la précédence de l'opérateur virgule ?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:854
|
|
|
|
|
msgid "Comma is not an operator in Python. Consider this session::"
|
|
|
|
|
msgstr ""
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"La virgule n'est pas un opérateur en Python. Observez la session suivante ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:859
|
|
|
|
|
msgid ""
|
|
|
|
|
"Since the comma is not an operator, but a separator between expressions the "
|
|
|
|
|
"above is evaluated as if you had entered::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comme la virgule n'est pas un opérateur, mais un séparateur entre deux "
|
|
|
|
|
"expression, l'expression ci dessus, est évaluée de la même façon que si vous "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"aviez écrit ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:864
|
|
|
|
|
msgid "not::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "et non ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:868
|
|
|
|
|
msgid ""
|
|
|
|
|
"The same is true of the various assignment operators (``=``, ``+=`` etc). "
|
|
|
|
|
"They are not truly operators but syntactic delimiters in assignment "
|
|
|
|
|
"statements."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Ceci est vrai pour tous les opérateurs d'assignations (``=``, ``+=`` etc). "
|
|
|
|
|
"Ce ne sont pas vraiment des opérateurs mais des délimiteurs syntaxiques dans "
|
|
|
|
|
"les instructions d'assignation."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:873
|
|
|
|
|
msgid "Is there an equivalent of C's \"?:\" ternary operator?"
|
|
|
|
|
msgstr "Existe-t-il un équivalent à l'opérateur ternaire \"?:\" du C ?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:875
|
|
|
|
|
msgid ""
|
|
|
|
|
"Yes, this feature was added in Python 2.5. The syntax would be as follows::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Oui, cette fonctionnalité à été ajouté à partir de Python 2.5. La syntaxe "
|
|
|
|
|
"est la suivante::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:883
|
|
|
|
|
msgid "For versions previous to 2.5 the answer would be 'No'."
|
|
|
|
|
msgstr "Pour les versions précédentes de python la réponse serait \"Non\"."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:887
|
|
|
|
|
msgid "Is it possible to write obfuscated one-liners in Python?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Est-il possible d'écrire des programmes obscurcis (*obfuscated*) d'une ligne "
|
|
|
|
|
"en Python ?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:889
|
|
|
|
|
msgid ""
|
|
|
|
|
"Yes. Usually this is done by nesting :keyword:`lambda` within :keyword:"
|
|
|
|
|
"`lambda`. See the following three examples, due to Ulf Bartelt::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Oui. Cela est généralement réalisé en imbriquant les :keyword:`lambda` dans "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"des :keyword:`lambda`. Observez les trois exemples suivants de Ulf Bartelt ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:914
|
|
|
|
|
msgid "Don't try this at home, kids!"
|
|
|
|
|
msgstr "Les enfants, ne faîtes pas ça chez vous !"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:918
|
|
|
|
|
msgid "Numbers and strings"
|
|
|
|
|
msgstr "Nombres et chaînes de caractères"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:921
|
|
|
|
|
msgid "How do I specify hexadecimal and octal integers?"
|
|
|
|
|
msgstr "Comment puis-je écrire des entiers hexadécimaux ou octaux ?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:923
|
|
|
|
|
msgid ""
|
|
|
|
|
"To specify an octal digit, precede the octal value with a zero, and then a "
|
|
|
|
|
"lower or uppercase \"o\". For example, to set the variable \"a\" to the "
|
|
|
|
|
"octal value \"10\" (8 in decimal), type::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Pour écrire un entier octal, faites précéder la valeur octale par un zéro, "
|
|
|
|
|
"puis un \"o\" majuscule ou minuscule. Par exemple assigner la valeur octale "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"\"10\" (8 en décimal) à la variable \"a\", tapez ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:931
|
|
|
|
|
msgid ""
|
|
|
|
|
"Hexadecimal is just as easy. Simply precede the hexadecimal number with a "
|
|
|
|
|
"zero, and then a lower or uppercase \"x\". Hexadecimal digits can be "
|
|
|
|
|
"specified in lower or uppercase. For example, in the Python interpreter::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"L'hexadécimal est tout aussi simple, faîtes précéder le nombre hexadécimal "
|
|
|
|
|
"par un zéro, puis un \"x\" majuscule ou minuscule. Les nombres hexadécimaux "
|
|
|
|
|
"peuvent être écrit en majuscules ou en minuscules. Par exemple, dans "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"l'interpréteur Python ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:944
|
|
|
|
|
msgid "Why does -22 // 10 return -3?"
|
2018-10-10 16:34:12 +00:00
|
|
|
|
msgstr "Pourquoi ``-22 // 10`` donne-t-il ``-3`` ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:946
|
|
|
|
|
msgid ""
|
|
|
|
|
"It's primarily driven by the desire that ``i % j`` have the same sign as "
|
|
|
|
|
"``j``. If you want that, and also want::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cela est principalement due à la volonté que ``i % j`` ait le même signe que "
|
|
|
|
|
"j. Si vous voulez cela, vous voulez aussi : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:951
|
|
|
|
|
msgid ""
|
|
|
|
|
"then integer division has to return the floor. C also requires that "
|
|
|
|
|
"identity to hold, and then compilers that truncate ``i // j`` need to make "
|
|
|
|
|
"``i % j`` have the same sign as ``i``."
|
|
|
|
|
msgstr ""
|
2017-10-23 20:33:03 +00:00
|
|
|
|
"Alors la division entière doit renvoyer l'entier inférieur. Le C demande "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"aussi à ce que cette égalité soit vérifiée, et donc les compilateur qui "
|
|
|
|
|
"tronquent ``i // j`` ont besoin que ``i % j`` ait le même signe que ``i``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:955
|
|
|
|
|
msgid ""
|
|
|
|
|
"There are few real use cases for ``i % j`` when ``j`` is negative. When "
|
|
|
|
|
"``j`` is positive, there are many, and in virtually all of them it's more "
|
|
|
|
|
"useful for ``i % j`` to be ``>= 0``. If the clock says 10 now, what did it "
|
|
|
|
|
"say 200 hours ago? ``-190 % 12 == 2`` is useful; ``-190 % 12 == -10`` is a "
|
|
|
|
|
"bug waiting to bite."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Il y a peu de cas d'utilisation réels pour ``i%j`` quand ``j`` est négatif. "
|
|
|
|
|
"Quand ``j`` est positif, il y en a beaucoup, et dans pratiquement tous, il "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"est plus utile que ``i % j`` soit ``>=0``. Si l'horloge dit *10h* "
|
|
|
|
|
"maintenant, que disait-elle il y a 200 heures? ``-190%12 == 2`` est utile; "
|
|
|
|
|
"``-192 % 12 == -10`` est un bug qui attends pour mordre."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:963
|
|
|
|
|
msgid ""
|
|
|
|
|
"On Python 2, ``a / b`` returns the same as ``a // b`` if ``__future__."
|
|
|
|
|
"division`` is not in effect. This is also known as \"classic\" division."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:969
|
|
|
|
|
msgid "How do I convert a string to a number?"
|
|
|
|
|
msgstr "Comment puis-je convertir une chaine de caractère en nombre?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:971
|
|
|
|
|
msgid ""
|
|
|
|
|
"For integers, use the built-in :func:`int` type constructor, e.g. "
|
|
|
|
|
"``int('144') == 144``. Similarly, :func:`float` converts to floating-point, "
|
|
|
|
|
"e.g. ``float('144') == 144.0``."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Pour les entiers, utilisez la fonction native :func:`int` de type "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"constructeur, par exemple ``int('144') == 144``. De façon similaire, :func:"
|
|
|
|
|
"`float` convertit en valeur flottante, par exemple ``float('144') == 144.0``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:975
|
|
|
|
|
msgid ""
|
|
|
|
|
"By default, these interpret the number as decimal, so that ``int('0144') == "
|
|
|
|
|
"144`` and ``int('0x144')`` raises :exc:`ValueError`. ``int(string, base)`` "
|
|
|
|
|
"takes the base to convert from as a second optional argument, so "
|
|
|
|
|
"``int('0x144', 16) == 324``. If the base is specified as 0, the number is "
|
|
|
|
|
"interpreted using Python's rules: a leading '0' indicates octal, and '0x' "
|
|
|
|
|
"indicates a hex number."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Par défaut, ces fonctions interprètent les nombre en tant que décimaux, de "
|
|
|
|
|
"telles façons que ``int('0144')==144`` et ``int('0x144')`` remontent :exc:"
|
|
|
|
|
"`ValueError`. ``int(string, base)`` prends la base depuis laquelle il faut "
|
|
|
|
|
"convertir dans le second argument, optionnel, donc ``int('0x144', 16) == "
|
|
|
|
|
"324``. Si la base donnée est 0, le nombre est interprété selon les règles "
|
|
|
|
|
"Python: un '0' en tête indique octal, et '0x' indique un hexadécimal."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:981
|
|
|
|
|
msgid ""
|
|
|
|
|
"Do not use the built-in function :func:`eval` if all you need is to convert "
|
|
|
|
|
"strings to numbers. :func:`eval` will be significantly slower and it "
|
|
|
|
|
"presents a security risk: someone could pass you a Python expression that "
|
|
|
|
|
"might have unwanted side effects. For example, someone could pass "
|
|
|
|
|
"``__import__('os').system(\"rm -rf $HOME\")`` which would erase your home "
|
|
|
|
|
"directory."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"N'utilisez pas la fonction native :func:`eval` si tout ce que vous avez "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"besoin est de convertir des chaines en nombres. :func:`eval` sera "
|
|
|
|
|
"significativement plus lent et implique des risque de sécurité: quelqu'un "
|
|
|
|
|
"pourrait vous envoyez une expression Python pouvant avoir des effets de bord "
|
|
|
|
|
"indésirables. Par exemple, quelqu'un pourrait passer ``__import__('os')."
|
|
|
|
|
"system(\"rm -rf $HOME\")`` ce qui aurait pour effet d'effacer votre "
|
|
|
|
|
"répertoire personnel."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:988
|
|
|
|
|
msgid ""
|
|
|
|
|
":func:`eval` also has the effect of interpreting numbers as Python "
|
|
|
|
|
"expressions, so that e.g. ``eval('09')`` gives a syntax error because Python "
|
|
|
|
|
"regards numbers starting with '0' as octal (base 8)."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:994
|
|
|
|
|
msgid "How do I convert a number to a string?"
|
|
|
|
|
msgstr "Comment convertir un nombre en chaine de caractère?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:996
|
|
|
|
|
msgid ""
|
|
|
|
|
"To convert, e.g., the number 144 to the string '144', use the built-in type "
|
|
|
|
|
"constructor :func:`str`. If you want a hexadecimal or octal representation, "
|
|
|
|
|
"use the built-in functions :func:`hex` or :func:`oct`. For fancy "
|
|
|
|
|
"formatting, see the :ref:`formatstrings` section, e.g. ``\"{:04d}\"."
|
|
|
|
|
"format(144)`` yields ``'0144'`` and ``\"{:.3f}\".format(1/3)`` yields "
|
|
|
|
|
"``'0.333'``. You may also use :ref:`the % operator <string-formatting>` on "
|
|
|
|
|
"strings. See the library reference manual for details."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1006
|
|
|
|
|
msgid "How do I modify a string in place?"
|
|
|
|
|
msgstr "Comment modifier une chaine de caractère \"en place\"?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1008
|
|
|
|
|
msgid ""
|
|
|
|
|
"You can't, because strings are immutable. If you need an object with this "
|
|
|
|
|
"ability, try converting the string to a list or use the array module::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Vous ne pouvez pas, par ce que les chaines de caractères sont immuables, Si "
|
|
|
|
|
"vous avez besoin d'un objet ayant une telle capacité, essayez de convertir "
|
|
|
|
|
"la chaine en liste, ou utilisez le module array::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1031
|
|
|
|
|
msgid "How do I use strings to call functions/methods?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment utiliser des chaines de caractères pour appeler des fonctions/"
|
|
|
|
|
"méthodes?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1033
|
|
|
|
|
msgid "There are various techniques."
|
|
|
|
|
msgstr "Il y a différentes techniques."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1035
|
|
|
|
|
msgid ""
|
|
|
|
|
"The best is to use a dictionary that maps strings to functions. The primary "
|
|
|
|
|
"advantage of this technique is that the strings do not need to match the "
|
|
|
|
|
"names of the functions. This is also the primary technique used to emulate "
|
|
|
|
|
"a case construct::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"La meilleure est d'utiliser un dictionnaire qui fait correspondre les "
|
|
|
|
|
"chaines de caractères à des fonctions. Le principal avantage de cette "
|
|
|
|
|
"technique est que les chaines n'ont pas besoin d'être égales aux noms de "
|
|
|
|
|
"fonctions. C'est aussi la principale façon d'imiter la construction \"case"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"\" ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1050
|
|
|
|
|
msgid "Use the built-in function :func:`getattr`::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Utiliser la fonction :func:`getattr` ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1055
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that :func:`getattr` works on any object, including classes, class "
|
|
|
|
|
"instances, modules, and so on."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Notez que :func:`getattr` marche sur n'importe quel objet, ceci inclue les "
|
|
|
|
|
"classes, les instances de classes, les modules et ainsi de suite."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1058
|
|
|
|
|
msgid "This is used in several places in the standard library, like this::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Ceci est utilisé dans plusieurs endroit de la bibliothèque standard, de "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"cette façon ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1071
|
|
|
|
|
msgid "Use :func:`locals` or :func:`eval` to resolve the function name::"
|
|
|
|
|
msgstr ""
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"Utilisez :func:`locals` ou :func:`eval` pour résoudre le nom de fonction ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1084
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note: Using :func:`eval` is slow and dangerous. If you don't have absolute "
|
|
|
|
|
"control over the contents of the string, someone could pass a string that "
|
|
|
|
|
"resulted in an arbitrary function being executed."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Note: En utilisant :func:`eval` est lent est dangereux. Si vous n'avez pas "
|
|
|
|
|
"un contrôle absolu sur le contenu de la chaine de caractère, quelqu'un peut "
|
|
|
|
|
"passer une chaine de caractère pouvant résulter en l'exécution de code "
|
|
|
|
|
"arbitraire."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1089
|
|
|
|
|
msgid ""
|
|
|
|
|
"Is there an equivalent to Perl's chomp() for removing trailing newlines from "
|
|
|
|
|
"strings?"
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Existe-t-il un équivalent à la fonction ``chomp()`` de Perl, pour retirer "
|
|
|
|
|
"les caractères de fin de ligne d'une chaine de caractère ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1091
|
|
|
|
|
msgid ""
|
|
|
|
|
"Starting with Python 2.2, you can use ``S.rstrip(\"\\r\\n\")`` to remove all "
|
|
|
|
|
"occurrences of any line terminator from the end of the string ``S`` without "
|
|
|
|
|
"removing other trailing whitespace. If the string ``S`` represents more "
|
|
|
|
|
"than one line, with several empty lines at the end, the line terminators for "
|
|
|
|
|
"all the blank lines will be removed::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1103
|
|
|
|
|
msgid ""
|
|
|
|
|
"Since this is typically only desired when reading text one line at a time, "
|
|
|
|
|
"using ``S.rstrip()`` this way works well."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Du fait que ce soit principalement utile en lisant un texte ligne à ligne, "
|
|
|
|
|
"utiliser ``S.rstrip()`` devrait marcher correctement."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1106
|
|
|
|
|
msgid "For older versions of Python, there are two partial substitutes:"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Pour les versions plus anciennes de python, il y a deux substituts partiels "
|
|
|
|
|
"disponibles."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1108
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you want to remove all trailing whitespace, use the ``rstrip()`` method "
|
|
|
|
|
"of string objects. This removes all trailing whitespace, not just a single "
|
|
|
|
|
"newline."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Si vous voulez retirer tous les espaces de fin de ligne, utilisez la méthode "
|
|
|
|
|
"``rstrip()`` des chaines de caractères. Cela retire tous les espaces de fin "
|
|
|
|
|
"de ligne, pas seulement le caractère de fin de ligne."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1112
|
|
|
|
|
msgid ""
|
|
|
|
|
"Otherwise, if there is only one line in the string ``S``, use ``S."
|
|
|
|
|
"splitlines()[0]``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Sinon, s'il y a seulement une ligne dans la chaine ``S``, utilisez ``S."
|
|
|
|
|
"splitlines()[0]``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1117
|
|
|
|
|
msgid "Is there a scanf() or sscanf() equivalent?"
|
2018-10-10 16:34:12 +00:00
|
|
|
|
msgstr "Existe-t-il un équivalent à ``scanf()`` ou ``sscanf()`` ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1119
|
|
|
|
|
msgid "Not as such."
|
|
|
|
|
msgstr "Pas exactement."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1121
|
|
|
|
|
msgid ""
|
|
|
|
|
"For simple input parsing, the easiest approach is usually to split the line "
|
|
|
|
|
"into whitespace-delimited words using the :meth:`~str.split` method of "
|
|
|
|
|
"string objects and then convert decimal strings to numeric values using :"
|
|
|
|
|
"func:`int` or :func:`float`. ``split()`` supports an optional \"sep\" "
|
|
|
|
|
"parameter which is useful if the line uses something other than whitespace "
|
|
|
|
|
"as a separator."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Pour une simple analyse de chaine, l'approche la plus simple est "
|
|
|
|
|
"généralement de découper la ligne en mots délimités par des espaces, en "
|
|
|
|
|
"utilisant la méthode :meth:`~str.split` des objets chaine de caractères, et "
|
|
|
|
|
"ensuite de convertir les chaines de décimales en valeurs numériques en "
|
|
|
|
|
"utilisant la fonction :func:`int` ou :func:`float`, ``split()`` supporte un "
|
|
|
|
|
"paramètre optionnel \"sep\" qui est utile si la ligne utilise autre chose "
|
|
|
|
|
"que des espaces comme séparateur."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1127
|
|
|
|
|
msgid ""
|
|
|
|
|
"For more complicated input parsing, regular expressions are more powerful "
|
|
|
|
|
"than C's :c:func:`sscanf` and better suited for the task."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Pour les analyses plus compliquées, les expressions rationnelles sont plus "
|
|
|
|
|
"puissantes que la fonction :c:func:`sscanf` de C et mieux adaptées à la "
|
|
|
|
|
"tâche."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1132
|
|
|
|
|
msgid ""
|
|
|
|
|
"What does 'UnicodeError: ASCII [decoding,encoding] error: ordinal not in "
|
|
|
|
|
"range(128)' mean?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1134
|
|
|
|
|
msgid ""
|
|
|
|
|
"This error indicates that your Python installation can handle only 7-bit "
|
|
|
|
|
"ASCII strings. There are a couple ways to fix or work around the problem."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1137
|
|
|
|
|
msgid ""
|
|
|
|
|
"If your programs must handle data in arbitrary character set encodings, the "
|
|
|
|
|
"environment the application runs in will generally identify the encoding of "
|
|
|
|
|
"the data it is handing you. You need to convert the input to Unicode data "
|
|
|
|
|
"using that encoding. For example, a program that handles email or web input "
|
|
|
|
|
"will typically find character set encoding information in Content-Type "
|
|
|
|
|
"headers. This can then be used to properly convert input data to Unicode. "
|
|
|
|
|
"Assuming the string referred to by ``value`` is encoded as UTF-8::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1147
|
|
|
|
|
msgid ""
|
|
|
|
|
"will return a Unicode object. If the data is not correctly encoded as "
|
|
|
|
|
"UTF-8, the above call will raise a :exc:`UnicodeError` exception."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1150
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you only want strings converted to Unicode which have non-ASCII data, you "
|
|
|
|
|
"can try converting them first assuming an ASCII encoding, and then generate "
|
|
|
|
|
"Unicode objects if that fails::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1162
|
|
|
|
|
msgid ""
|
|
|
|
|
"It's possible to set a default encoding in a file called ``sitecustomize."
|
|
|
|
|
"py`` that's part of the Python library. However, this isn't recommended "
|
|
|
|
|
"because changing the Python-wide default encoding may cause third-party "
|
|
|
|
|
"extension modules to fail."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1167
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that on Windows, there is an encoding known as \"mbcs\", which uses an "
|
|
|
|
|
"encoding specific to your current locale. In many cases, and particularly "
|
|
|
|
|
"when working with COM, this may be an appropriate default encoding to use."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1173
|
|
|
|
|
msgid "Sequences (Tuples/Lists)"
|
|
|
|
|
msgstr "Sequences (Tuples/Lists)"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1176
|
|
|
|
|
msgid "How do I convert between tuples and lists?"
|
|
|
|
|
msgstr "Comment convertir les listes en tuples et inversement?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1178
|
|
|
|
|
msgid ""
|
|
|
|
|
"The type constructor ``tuple(seq)`` converts any sequence (actually, any "
|
|
|
|
|
"iterable) into a tuple with the same items in the same order."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Le constructeur de type ``tuple(seq)`` convertit toute séquence (en fait "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"tout itérable) en un tuple avec les mêmes éléments dans le même ordre…."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1181
|
|
|
|
|
msgid ""
|
|
|
|
|
"For example, ``tuple([1, 2, 3])`` yields ``(1, 2, 3)`` and ``tuple('abc')`` "
|
|
|
|
|
"yields ``('a', 'b', 'c')``. If the argument is a tuple, it does not make a "
|
|
|
|
|
"copy but returns the same object, so it is cheap to call :func:`tuple` when "
|
|
|
|
|
"you aren't sure that an object is already a tuple."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Par exemple ``tuple([1, 2, 3])`` renvoi ``(1, 2, 3)`` et ``tuple('abc')`` "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"renvoi ``('a', 'b', 'c')``. Si l'argument est un tuple, cela ne crée pas une "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"copie, mais renvoi le même objet, ce qui fait de :func:`tuple` un fonction "
|
|
|
|
|
"économique à appeler quand vous ne savez pas si votre objet est déjà un "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"tuple."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1186
|
|
|
|
|
msgid ""
|
|
|
|
|
"The type constructor ``list(seq)`` converts any sequence or iterable into a "
|
|
|
|
|
"list with the same items in the same order. For example, ``list((1, 2, "
|
|
|
|
|
"3))`` yields ``[1, 2, 3]`` and ``list('abc')`` yields ``['a', 'b', 'c']``. "
|
|
|
|
|
"If the argument is a list, it makes a copy just like ``seq[:]`` would."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Le constructeur de type ``list(seq)`` convertit toute séquence ou itérable "
|
|
|
|
|
"en liste contenant les mêmes éléments dans le même ordre. Par exemple, "
|
2017-10-23 20:33:03 +00:00
|
|
|
|
"``list((1,2,3))`` renvoie ``[1,2,3]`` et ``list('abc')`` renvoie "
|
|
|
|
|
"``['a','b','c']``. Si l'argument est une liste, il renvoie une copie, de la "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"même façon que ``seq[:]``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1193
|
|
|
|
|
msgid "What's a negative index?"
|
|
|
|
|
msgstr "Qu'est-ce qu'un indexe négatif?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1195
|
|
|
|
|
msgid ""
|
|
|
|
|
"Python sequences are indexed with positive numbers and negative numbers. "
|
|
|
|
|
"For positive numbers 0 is the first index 1 is the second index and so "
|
|
|
|
|
"forth. For negative indices -1 is the last index and -2 is the penultimate "
|
|
|
|
|
"(next to last) index and so forth. Think of ``seq[-n]`` as the same as "
|
|
|
|
|
"``seq[len(seq)-n]``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Les séquences Python sont indexées avec des nombres positifs aussi bien que "
|
|
|
|
|
"négatifs. Pour les nombres positifs, 0 est le premier index, 1 est le "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"second, et ainsi de suite. Pour les indexes négatifs, ``-1`` est le dernier "
|
|
|
|
|
"index, ``-2`` est le pénultième (avant dernier), et ainsi de suite. On peut "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"aussi dire que ``seq[-n]`` est équivalent à ``seq[len(seq)-n]``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1200
|
|
|
|
|
msgid ""
|
|
|
|
|
"Using negative indices can be very convenient. For example ``S[:-1]`` is "
|
|
|
|
|
"all of the string except for its last character, which is useful for "
|
|
|
|
|
"removing the trailing newline from a string."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Utiliser des indexes négatifs peut être très pratique. Par exemple "
|
|
|
|
|
"``S[:-1]`` indique la chaine entière a l'exception du dernier caractère, ce "
|
|
|
|
|
"qui est pratique pour retirer un caractère de fin de ligne en fin d'une "
|
|
|
|
|
"chaine."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1206
|
|
|
|
|
msgid "How do I iterate over a sequence in reverse order?"
|
|
|
|
|
msgstr "Comment itérer à rebours sur une séquence?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1208
|
|
|
|
|
msgid ""
|
|
|
|
|
"Use the :func:`reversed` built-in function, which is new in Python 2.4::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Utilisez la fonction embarquée :func:`reversed`, qui est apparue en Python "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"2.4 ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1213
|
|
|
|
|
msgid ""
|
|
|
|
|
"This won't touch your original sequence, but build a new copy with reversed "
|
|
|
|
|
"order to iterate over."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cela ne modifiera pas votre séquence initiale, mais construira à la place "
|
|
|
|
|
"une copie en ordre inverse pour itérer dessus."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1216
|
|
|
|
|
msgid "With Python 2.3, you can use an extended slice syntax::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Avec Python 2.3 vous pouvez utiliser la syntaxe étendue de tranches ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1223
|
|
|
|
|
msgid "How do you remove duplicates from a list?"
|
|
|
|
|
msgstr "Comment retirer les doublons d'une liste?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1225
|
|
|
|
|
msgid "See the Python Cookbook for a long discussion of many ways to do this:"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Lisez le Python Cookbook pour trouver une longue discussion sur les "
|
|
|
|
|
"nombreuses façons de faire cela:"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1227
|
|
|
|
|
msgid "https://code.activestate.com/recipes/52560/"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "https://code.activestate.com/recipes/52560/"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1229
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you don't mind reordering the list, sort it and then scan from the end of "
|
|
|
|
|
"the list, deleting duplicates as you go::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Si changer l'ordre de la liste ne vous dérange pas, commencez par trier "
|
|
|
|
|
"celle ci, puis parcourez la d'un bout à l'autre, en supprimant les doublons "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"trouvés en chemin ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1241
|
|
|
|
|
msgid ""
|
|
|
|
|
"If all elements of the list may be used as dictionary keys (i.e. they are "
|
|
|
|
|
"all hashable) this is often faster ::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Si tous les éléments de la liste peuvent être utilisés comme des clés de "
|
|
|
|
|
"dictionnaire (cad, elles sont toutes hashables) ceci est souvent plus "
|
|
|
|
|
"rapide::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1249
|
|
|
|
|
msgid "In Python 2.5 and later, the following is possible instead::"
|
|
|
|
|
msgstr "En Python 2.5 et suivant, la forme suivante est possible à la place::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1253
|
|
|
|
|
msgid ""
|
|
|
|
|
"This converts the list into a set, thereby removing duplicates, and then "
|
|
|
|
|
"back into a list."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Ceci convertis la liste en un ensemble, ce qui supprime automatiquement les "
|
|
|
|
|
"doublons, puis la transforme à nouveau en liste."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1258
|
|
|
|
|
msgid "How do you make an array in Python?"
|
|
|
|
|
msgstr "Comment construire un tableau en Python?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1260
|
|
|
|
|
msgid "Use a list::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Utilisez une liste ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1264
|
|
|
|
|
msgid ""
|
|
|
|
|
"Lists are equivalent to C or Pascal arrays in their time complexity; the "
|
|
|
|
|
"primary difference is that a Python list can contain objects of many "
|
|
|
|
|
"different types."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Les listes ont un cout équivalent à celui des tableau C ou Pascal; la "
|
|
|
|
|
"principale différence est qu'une liste Python peut contenir des objets de "
|
|
|
|
|
"différents types."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1267
|
|
|
|
|
msgid ""
|
|
|
|
|
"The ``array`` module also provides methods for creating arrays of fixed "
|
|
|
|
|
"types with compact representations, but they are slower to index than "
|
|
|
|
|
"lists. Also note that the Numeric extensions and others define array-like "
|
|
|
|
|
"structures with various characteristics as well."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Le module ``array`` fournit des méthodes pour créer des tableaux de types "
|
|
|
|
|
"fixes dans une représentation compacte, mais ils sont plus lents à indexer "
|
|
|
|
|
"que les listes. Notez aussi que l'extension ``Numeric`` et d'autres, "
|
|
|
|
|
"fournissent différentes structures de types tableaux, avec des "
|
|
|
|
|
"caractéristiques différentes."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1272
|
|
|
|
|
msgid ""
|
|
|
|
|
"To get Lisp-style linked lists, you can emulate cons cells using tuples::"
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Pour obtenir des listes chainées de type Lisp, vous pouvez émuler les *cons "
|
|
|
|
|
"cells* en utilisant des tuples ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1276
|
|
|
|
|
msgid ""
|
|
|
|
|
"If mutability is desired, you could use lists instead of tuples. Here the "
|
|
|
|
|
"analogue of lisp car is ``lisp_list[0]`` and the analogue of cdr is "
|
|
|
|
|
"``lisp_list[1]``. Only do this if you're sure you really need to, because "
|
|
|
|
|
"it's usually a lot slower than using Python lists."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Si vous voulez pouvoir modifier les éléments, utilisez une liste plutôt "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"qu'un tuple. Ici la version équivalente au *car* de Lisp est "
|
|
|
|
|
"``lisp_list[0]`` et l'équivalent à *cdr* est ``list_lip[1]``. Ne faites ceci "
|
|
|
|
|
"que si vous êtes réellement sûr d'en avoir besoin, cette méthode est en "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"générale bien plus lente que les listes Python."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1285
|
|
|
|
|
msgid "How do I create a multidimensional list?"
|
|
|
|
|
msgstr "Comment puis-je créer une liste à plusieurs dimensions?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1287
|
|
|
|
|
msgid "You probably tried to make a multidimensional array like this::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Vous avez probablement essayé de créer une liste à plusieurs dimensions de "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"cette façon ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1291
|
|
|
|
|
msgid "This looks correct if you print it::"
|
|
|
|
|
msgstr "Cela semble correct quand vous essayer de l'afficher::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1296
|
|
|
|
|
msgid "But when you assign a value, it shows up in multiple places:"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Mais quand vous assignez une valeur, elle apparait en de multiples endroits::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1302
|
|
|
|
|
msgid ""
|
|
|
|
|
"The reason is that replicating a list with ``*`` doesn't create copies, it "
|
|
|
|
|
"only creates references to the existing objects. The ``*3`` creates a list "
|
|
|
|
|
"containing 3 references to the same list of length two. Changes to one row "
|
|
|
|
|
"will show in all rows, which is almost certainly not what you want."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"La raison en est que dupliquer une liste en utilisant ``*`` ne crée pas de "
|
|
|
|
|
"copies, cela crée seulement des références aux objets existants. Le ``*3`` "
|
|
|
|
|
"crée une liste contenant trois références à la même liste de longueur deux. "
|
|
|
|
|
"Un changement dans une colonne apparaîtra donc dans toutes les colonnes. Ce "
|
|
|
|
|
"qui n'est de façon quasi certaine, pas ce que vous souhaitez."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1307
|
|
|
|
|
msgid ""
|
|
|
|
|
"The suggested approach is to create a list of the desired length first and "
|
|
|
|
|
"then fill in each element with a newly created list::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"L'approche suggérée est de créer une liste de la longueur désiré d'abords, "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"puis de remplir tous les éléments avec une chaîne nouvellement créée ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1314
|
|
|
|
|
msgid ""
|
|
|
|
|
"This generates a list containing 3 different lists of length two. You can "
|
|
|
|
|
"also use a list comprehension::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cette liste générée contient trois listes différentes de longueur deux. Vous "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"pouvez aussi utilisez la notation de compréhension de listes ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1320
|
|
|
|
|
msgid ""
|
|
|
|
|
"Or, you can use an extension that provides a matrix datatype; `NumPy <http://"
|
|
|
|
|
"www.numpy.org/>`_ is the best known."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Vous pouvez aussi utiliser une extension qui fournit un type matriciel "
|
|
|
|
|
"natif ; `NumPy <http://www.numpy.org/>`_ est la plus répandue."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1325
|
|
|
|
|
msgid "How do I apply a method to a sequence of objects?"
|
|
|
|
|
msgstr "Comment appliquer une méthode à une séquence d'objets?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1327
|
|
|
|
|
msgid "Use a list comprehension::"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
msgstr "Utilisez une compréhension de liste ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1331
|
|
|
|
|
msgid "More generically, you can try the following function::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1341
|
|
|
|
|
msgid ""
|
|
|
|
|
"Why does a_tuple[i] += ['item'] raise an exception when the addition works?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pourquoi a_tuple[i] += ['item'] lève-t'il une exception alors que "
|
|
|
|
|
"l'addition fonctionne ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1343
|
|
|
|
|
msgid ""
|
|
|
|
|
"This is because of a combination of the fact that augmented assignment "
|
|
|
|
|
"operators are *assignment* operators, and the difference between mutable and "
|
|
|
|
|
"immutable objects in Python."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Ceci est dû à la combinaison de deux facteurs : le fait que les opérateurs "
|
|
|
|
|
"d'affectation incrémentaux sont des opérateurs d'*affectation* et à la "
|
|
|
|
|
"différence entre les objets muables et immuables en Python."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1347
|
|
|
|
|
msgid ""
|
|
|
|
|
"This discussion applies in general when augmented assignment operators are "
|
|
|
|
|
"applied to elements of a tuple that point to mutable objects, but we'll use "
|
|
|
|
|
"a ``list`` and ``+=`` as our exemplar."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Cette discussion est valable, en général, quand des opérateurs d'affectation "
|
|
|
|
|
"incrémentale sont appliqués aux élément d'un n-uplet qui pointe sur des "
|
|
|
|
|
"objets muables, mais on prendra ``list`` et ``+=`` comme exemple."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1351
|
|
|
|
|
msgid "If you wrote::"
|
|
|
|
|
msgstr "Si vous écrivez : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1359
|
|
|
|
|
msgid ""
|
|
|
|
|
"The reason for the exception should be immediately clear: ``1`` is added to "
|
|
|
|
|
"the object ``a_tuple[0]`` points to (``1``), producing the result object, "
|
|
|
|
|
"``2``, but when we attempt to assign the result of the computation, ``2``, "
|
|
|
|
|
"to element ``0`` of the tuple, we get an error because we can't change what "
|
|
|
|
|
"an element of a tuple points to."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"La cause de l'exception est claire : ``1`` est ajouté à l'objet "
|
|
|
|
|
"``a_tuple[0]`` qui pointe sur (``1``), ce qui produit l'objet résultant "
|
|
|
|
|
"``2``, mais, lorsque l'on tente d'affecter le résultat du calcul, ``2``, à "
|
|
|
|
|
"l'élément ``0`` du n-uplet, on obtient une erreur car il est impossible de "
|
|
|
|
|
"modifier la cible sur laquelle pointe un élément d'un n-uplet."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1365
|
|
|
|
|
msgid ""
|
|
|
|
|
"Under the covers, what this augmented assignment statement is doing is "
|
|
|
|
|
"approximately this::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Sous le capot, une instruction d'affectation incrémentale fait à peu près "
|
|
|
|
|
"ceci :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1374
|
|
|
|
|
msgid ""
|
|
|
|
|
"It is the assignment part of the operation that produces the error, since a "
|
|
|
|
|
"tuple is immutable."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"C'est la partie de l'affectation de l'opération qui génère l'erreur, vu "
|
|
|
|
|
"qu'un n-uplet est immuable."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1377
|
|
|
|
|
msgid "When you write something like::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Quand vous écrivez un code du style :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1385
|
|
|
|
|
msgid ""
|
|
|
|
|
"The exception is a bit more surprising, and even more surprising is the fact "
|
|
|
|
|
"that even though there was an error, the append worked::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"L'exception est un peu plus surprenante et, chose encore plus étrange, "
|
|
|
|
|
"malgré l'erreur, l'ajout a fonctionné ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1391
|
|
|
|
|
msgid ""
|
|
|
|
|
"To see why this happens, you need to know that (a) if an object implements "
|
|
|
|
|
"an ``__iadd__`` magic method, it gets called when the ``+=`` augmented "
|
|
|
|
|
"assignment is executed, and its return value is what gets used in the "
|
|
|
|
|
"assignment statement; and (b) for lists, ``__iadd__`` is equivalent to "
|
|
|
|
|
"calling ``extend`` on the list and returning the list. That's why we say "
|
|
|
|
|
"that for lists, ``+=`` is a \"shorthand\" for ``list.extend``::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pour comprendre ce qui se passe, il faut savoir que, premièrement, si un "
|
|
|
|
|
"objet implémente la méthode magique c, celle-ci est appelée quand "
|
|
|
|
|
"l'affectation incrémentale ``+=`` est exécutée et sa valeur de retour est "
|
|
|
|
|
"utilisée dans l'instruction d'affectation ; et que, deuxièmement, pour les "
|
|
|
|
|
"listes, ``__iadd__`` équivaut à appeler ``extend`` sur la liste et à "
|
|
|
|
|
"renvoyer celle-ci. C'est pour cette raison que l'on dit que pour les listes, "
|
|
|
|
|
"``+=`` est un \"raccourci\" pour ``list.extend``::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1403
|
|
|
|
|
msgid "This is equivalent to::"
|
|
|
|
|
msgstr "C’est équivalent à ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1408
|
|
|
|
|
msgid ""
|
|
|
|
|
"The object pointed to by a_list has been mutated, and the pointer to the "
|
|
|
|
|
"mutated object is assigned back to ``a_list``. The end result of the "
|
|
|
|
|
"assignment is a no-op, since it is a pointer to the same object that "
|
|
|
|
|
"``a_list`` was previously pointing to, but the assignment still happens."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"L'objet sur lequel pointe ``a_list`` a été modifié et le pointeur vers "
|
|
|
|
|
"l'objet modifié est réaffecté à ``a_list``. *In fine*, l'affectation ne "
|
|
|
|
|
"change rien, puisque c'est un pointeur vers le même objet que sur lequel "
|
|
|
|
|
"pointait ``a_list``, mais l'affectation a tout de même lieu."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1413
|
|
|
|
|
msgid "Thus, in our tuple example what is happening is equivalent to::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Donc, dans notre exemple avec un n-uplet, il se passe quelque chose "
|
|
|
|
|
"équivalent à ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1421
|
|
|
|
|
msgid ""
|
|
|
|
|
"The ``__iadd__`` succeeds, and thus the list is extended, but even though "
|
|
|
|
|
"``result`` points to the same object that ``a_tuple[0]`` already points to, "
|
|
|
|
|
"that final assignment still results in an error, because tuples are "
|
|
|
|
|
"immutable."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"L'appel à ``__iadd__`` réussit et la liste est étendue, mais bien que "
|
|
|
|
|
"``result`` pointe sur le même objet que ``a_tuple[0]``, l'affectation finale "
|
|
|
|
|
"échoue car les n-uplets ne sont pas muables."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1427
|
|
|
|
|
msgid "Dictionaries"
|
|
|
|
|
msgstr "Dictionnaires"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1430
|
|
|
|
|
msgid "How can I get a dictionary to display its keys in a consistent order?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment puis-je faire afficher les éléments d'un dictionnaire dans un ordre "
|
|
|
|
|
"consistant?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1432
|
|
|
|
|
msgid ""
|
|
|
|
|
"You can't. Dictionaries store their keys in an unpredictable order, so the "
|
|
|
|
|
"display order of a dictionary's elements will be similarly unpredictable."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Vous ne pouvez pas. Les dictionnaires enregistrent leurs clées dans un ordre "
|
|
|
|
|
"non prévisible, l'ordre d'affichage des éléments d'un dictionnaire sera donc "
|
|
|
|
|
"de la même façon imprévisible."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1435
|
|
|
|
|
msgid ""
|
|
|
|
|
"This can be frustrating if you want to save a printable version to a file, "
|
|
|
|
|
"make some changes and then compare it with some other printed dictionary. "
|
|
|
|
|
"In this case, use the ``pprint`` module to pretty-print the dictionary; the "
|
|
|
|
|
"items will be presented in order sorted by the key."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cela peut être frustrant si vous voulez sauvegarder une version affichable "
|
|
|
|
|
"dans un fichier, faire des changement puis comparer avec un autre "
|
|
|
|
|
"dictionnaire affiché. Dans ce cas, utilisez le module ``pprint``` pour "
|
|
|
|
|
"afficher joliement le dictionnaire; les éléments seront présentés triés par "
|
|
|
|
|
"clés."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1440
|
|
|
|
|
msgid ""
|
|
|
|
|
"A more complicated solution is to subclass ``dict`` to create a "
|
|
|
|
|
"``SortedDict`` class that prints itself in a predictable order. Here's one "
|
|
|
|
|
"simpleminded implementation of such a class::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Une solution plus compliquée est de sousclasser ``dict`` pour créer une "
|
|
|
|
|
"classe``SorterDict`` qui s'affiche de façon prévisible. Voici une "
|
|
|
|
|
"implémentation simple d'une telle classe::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1452
|
|
|
|
|
msgid ""
|
|
|
|
|
"This will work for many common situations you might encounter, though it's "
|
|
|
|
|
"far from a perfect solution. The largest flaw is that if some values in the "
|
|
|
|
|
"dictionary are also dictionaries, their values won't be presented in any "
|
|
|
|
|
"particular order."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cela marchera dans la plupart des situations que vous pourriez rencontrer, "
|
|
|
|
|
"même si c'est loin d'être une solution parfaite. Le plus gros problème avec "
|
|
|
|
|
"cette solution est que si certaines valeurs dans le dictionnaire sont aussi "
|
|
|
|
|
"des dictionnaire, alors elles ne seront pas présentées dans un ordre "
|
|
|
|
|
"particulier."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1459
|
|
|
|
|
msgid ""
|
|
|
|
|
"I want to do a complicated sort: can you do a Schwartzian Transform in "
|
|
|
|
|
"Python?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Je souhaite faire un tri compliqué: peut on faire une transformation de "
|
|
|
|
|
"Schwartz en Python?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1461
|
|
|
|
|
msgid ""
|
|
|
|
|
"The technique, attributed to Randal Schwartz of the Perl community, sorts "
|
|
|
|
|
"the elements of a list by a metric which maps each element to its \"sort "
|
|
|
|
|
"value\". In Python, use the ``key`` argument for the :func:`sort()` "
|
|
|
|
|
"function::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1470
|
|
|
|
|
msgid "How can I sort one list by values from another list?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment puis-je trier une liste en fonction des valeurs d'une autre liste?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1472
|
|
|
|
|
msgid ""
|
|
|
|
|
"Merge them into a single list of tuples, sort the resulting list, and then "
|
|
|
|
|
"pick out the element you want. ::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1485
|
|
|
|
|
msgid "An alternative for the last step is::"
|
|
|
|
|
msgstr "Une alternative pour la dernière étape est : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1490
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you find this more legible, you might prefer to use this instead of the "
|
|
|
|
|
"final list comprehension. However, it is almost twice as slow for long "
|
|
|
|
|
"lists. Why? First, the ``append()`` operation has to reallocate memory, and "
|
|
|
|
|
"while it uses some tricks to avoid doing that each time, it still has to do "
|
|
|
|
|
"it occasionally, and that costs quite a bit. Second, the expression "
|
|
|
|
|
"\"result.append\" requires an extra attribute lookup, and third, there's a "
|
|
|
|
|
"speed reduction from having to make all those function calls."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Si vous trouvez cela plus lisible, vous préférez peut-être utiliser ceci à "
|
|
|
|
|
"la place de la compréhension de la liste finale. Toutefois, ceci est presque "
|
|
|
|
|
"deux fois plus lent pour les longues listes. Pourquoi? Tout d'abord, "
|
|
|
|
|
"``append ()`` doit réaffecter la mémoire, et si il utilise quelques astuces "
|
|
|
|
|
"pour éviter de le faire à chaque fois, il doit encore le faire de temps en "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"temps, ce qui coûte assez cher. Deuxièmement, l'expression ``result.append`` "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"exige une recherche d'attribut supplémentaire, et enfin, tous ces appels de "
|
|
|
|
|
"fonction impactent la vitesse d'exécution."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1500
|
|
|
|
|
msgid "Objects"
|
|
|
|
|
msgstr "Objets"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1503
|
|
|
|
|
msgid "What is a class?"
|
|
|
|
|
msgstr "Qu'est-ce qu'une classe?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1505
|
|
|
|
|
msgid ""
|
|
|
|
|
"A class is the particular object type created by executing a class "
|
|
|
|
|
"statement. Class objects are used as templates to create instance objects, "
|
|
|
|
|
"which embody both the data (attributes) and code (methods) specific to a "
|
|
|
|
|
"datatype."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Une classe est le type d'objet particulier créé par l'exécution d'une "
|
|
|
|
|
"déclaration de classe. Les objets de classe sont utilisés comme modèles pour "
|
|
|
|
|
"créer des objets, qui incarnent à la fois les données (attributs) et le code "
|
|
|
|
|
"(méthodes) spécifiques à un type de données."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1509
|
|
|
|
|
msgid ""
|
|
|
|
|
"A class can be based on one or more other classes, called its base "
|
|
|
|
|
"class(es). It then inherits the attributes and methods of its base classes. "
|
|
|
|
|
"This allows an object model to be successively refined by inheritance. You "
|
|
|
|
|
"might have a generic ``Mailbox`` class that provides basic accessor methods "
|
|
|
|
|
"for a mailbox, and subclasses such as ``MboxMailbox``, ``MaildirMailbox``, "
|
|
|
|
|
"``OutlookMailbox`` that handle various specific mailbox formats."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Une classe peut être fondée sur une ou plusieurs autres classes, appelée sa "
|
|
|
|
|
"ou ses classes de base. Il hérite alors les attributs et les méthodes de ses "
|
|
|
|
|
"classes de base. Cela permet à un modèle d'objet d'être successivement "
|
|
|
|
|
"raffinés par héritage. Vous pourriez avoir une classe générique ``Mailbox`` "
|
|
|
|
|
"qui fournit des méthodes d'accès de base pour une boîte aux lettres, et sous-"
|
|
|
|
|
"classes telles que ``MboxMailbox``, ``MaildirMailbox``, ``OutlookMailbox`` "
|
|
|
|
|
"qui gèrent les différents formats de boîtes aux lettres spécifiques."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1518
|
|
|
|
|
msgid "What is a method?"
|
|
|
|
|
msgstr "Qu'est-ce qu'une méthode?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1520
|
|
|
|
|
msgid ""
|
|
|
|
|
"A method is a function on some object ``x`` that you normally call as ``x."
|
|
|
|
|
"name(arguments...)``. Methods are defined as functions inside the class "
|
|
|
|
|
"definition::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Une méthode est une fonction sur un objet ``x`` appelez normalement comme "
|
|
|
|
|
"``x.name(arguments…)``. Les méthodes sont définies comme des fonctions à "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"l'intérieur de la définition de classe ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1530
|
|
|
|
|
msgid "What is self?"
|
|
|
|
|
msgstr "Qu'est-ce que self?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1532
|
|
|
|
|
msgid ""
|
|
|
|
|
"Self is merely a conventional name for the first argument of a method. A "
|
|
|
|
|
"method defined as ``meth(self, a, b, c)`` should be called as ``x.meth(a, b, "
|
|
|
|
|
"c)`` for some instance ``x`` of the class in which the definition occurs; "
|
|
|
|
|
"the called method will think it is called as ``meth(x, a, b, c)``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Self est simplement un nom conventionnel pour le premier argument d'une "
|
|
|
|
|
"méthode. Une méthode définie comme ``meth(self, a, b, c)`` doit être appelée "
|
|
|
|
|
"en tant que ``x.meth(a, b, c)``, pour une instance ``x`` de la classe dans "
|
|
|
|
|
"laquelle elle est définie, la méthode appelée considérera qu'elle est "
|
|
|
|
|
"appelée ``meth(x, a, b, c)``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1537
|
|
|
|
|
msgid "See also :ref:`why-self`."
|
|
|
|
|
msgstr "Voir aussi :ref:`why-self`."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1541
|
|
|
|
|
msgid ""
|
|
|
|
|
"How do I check if an object is an instance of a given class or of a subclass "
|
|
|
|
|
"of it?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment puis-je vérifier si un objet est une instance d'une classe donnée ou "
|
|
|
|
|
"d'une sous-classe de celui-ci?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1543
|
|
|
|
|
msgid ""
|
|
|
|
|
"Use the built-in function ``isinstance(obj, cls)``. You can check if an "
|
|
|
|
|
"object is an instance of any of a number of classes by providing a tuple "
|
|
|
|
|
"instead of a single class, e.g. ``isinstance(obj, (class1, class2, ...))``, "
|
|
|
|
|
"and can also check whether an object is one of Python's built-in types, e.g. "
|
|
|
|
|
"``isinstance(obj, str)`` or ``isinstance(obj, (int, long, float, complex))``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1549
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that most programs do not use :func:`isinstance` on user-defined "
|
|
|
|
|
"classes very often. If you are developing the classes yourself, a more "
|
|
|
|
|
"proper object-oriented style is to define methods on the classes that "
|
|
|
|
|
"encapsulate a particular behaviour, instead of checking the object's class "
|
|
|
|
|
"and doing a different thing based on what class it is. For example, if you "
|
|
|
|
|
"have a function that does something::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Notez que la plupart des programmes n'utilisent pas :func:`isInstance` sur "
|
|
|
|
|
"les classes définies par l'utilisateur, très souvent. Si vous développez "
|
|
|
|
|
"vous-même les classes, un style plus appropriée orientée objet est de "
|
|
|
|
|
"définir des méthodes sur les classes qui encapsulent un comportement "
|
|
|
|
|
"particulier, au lieu de vérifier la classe de l'objet et de faire quelque "
|
|
|
|
|
"chose de différent en fonction de sa classe. Par exemple, si vous avez une "
|
|
|
|
|
"fonction qui fait quelque chose : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1563
|
|
|
|
|
msgid ""
|
|
|
|
|
"A better approach is to define a ``search()`` method on all the classes and "
|
|
|
|
|
"just call it::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Une meilleure approche est de définir une méthode ``search()`` sur toutes "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"les classes et qu'il suffit d'appeler ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1578
|
|
|
|
|
msgid "What is delegation?"
|
|
|
|
|
msgstr "Qu'est-ce que la délégation?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1580
|
|
|
|
|
msgid ""
|
|
|
|
|
"Delegation is an object oriented technique (also called a design pattern). "
|
|
|
|
|
"Let's say you have an object ``x`` and want to change the behaviour of just "
|
|
|
|
|
"one of its methods. You can create a new class that provides a new "
|
|
|
|
|
"implementation of the method you're interested in changing and delegates all "
|
|
|
|
|
"other methods to the corresponding method of ``x``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"La délégation est une technique orientée objet (aussi appelé un modèle de "
|
|
|
|
|
"conception). Disons que vous avez un objet ``x`` et que vous souhaitez "
|
|
|
|
|
"modifier le comportement d'une seule de ses méthodes. Vous pouvez créer une "
|
|
|
|
|
"nouvelle classe qui fournit une nouvelle implémentation de la méthode qui "
|
|
|
|
|
"vous intéresse dans l'évolution et les délégués de toutes les autres "
|
|
|
|
|
"méthodes la méthode correspondante de ``x``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1586
|
|
|
|
|
msgid ""
|
|
|
|
|
"Python programmers can easily implement delegation. For example, the "
|
|
|
|
|
"following class implements a class that behaves like a file but converts all "
|
|
|
|
|
"written data to uppercase::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Les programmeurs Python peuvent facilement mettre en œuvre la délégation. "
|
|
|
|
|
"Par exemple, la classe suivante implémente une classe qui se comporte comme "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"un fichier, mais convertit toutes les données écrites en majuscules ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1601
|
|
|
|
|
msgid ""
|
|
|
|
|
"Here the ``UpperOut`` class redefines the ``write()`` method to convert the "
|
|
|
|
|
"argument string to uppercase before calling the underlying ``self.__outfile."
|
|
|
|
|
"write()`` method. All other methods are delegated to the underlying ``self."
|
|
|
|
|
"__outfile`` object. The delegation is accomplished via the ``__getattr__`` "
|
|
|
|
|
"method; consult :ref:`the language reference <attribute-access>` for more "
|
|
|
|
|
"information about controlling attribute access."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Ici, la classe ``UpperOut`` redéfinit la méthode ``write()`` pour convertir "
|
|
|
|
|
"la chaîne d'argument en majuscules avant d'appeler la méthode sous-jacentes "
|
|
|
|
|
"``self.__outfile.write()``. Toutes les autres méthodes sont déléguées à "
|
|
|
|
|
"l'objet sous-jacent ``self.__outfile``. La délégation se fait par la méthode "
|
|
|
|
|
"``__getattr__``, consulter :ref:`the language reference <attribute-access>` "
|
|
|
|
|
"pour plus d'informations sur le contrôle d'accès d'attribut."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1608
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that for more general cases delegation can get trickier. When "
|
|
|
|
|
"attributes must be set as well as retrieved, the class must define a :meth:"
|
|
|
|
|
"`__setattr__` method too, and it must do so carefully. The basic "
|
|
|
|
|
"implementation of :meth:`__setattr__` is roughly equivalent to the "
|
|
|
|
|
"following::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Notez que pour une utilisation plus générale de la délégation, les choses "
|
|
|
|
|
"peuvent se compliquer. Lorsque les attributs doivent être définis aussi bien "
|
|
|
|
|
"que récupérés, la classe doit définir une méthode :meth:`__setattr__` aussi, "
|
|
|
|
|
"et il doit le faire avec soin. La mise en œuvre basique de la méthode :meth:"
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"`__setattr__` est à peu près équivalent à ce qui suit ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1619
|
|
|
|
|
msgid ""
|
|
|
|
|
"Most :meth:`__setattr__` implementations must modify ``self.__dict__`` to "
|
|
|
|
|
"store local state for self without causing an infinite recursion."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"La plupart des implémentations de :meth:`__setattr__` doivent modifier "
|
|
|
|
|
"``self.__dict__`` pour stocker l'état locale de self sans provoquer une "
|
|
|
|
|
"récursion infinie."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1624
|
|
|
|
|
msgid ""
|
|
|
|
|
"How do I call a method defined in a base class from a derived class that "
|
|
|
|
|
"overrides it?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment appeler une méthode définie dans une classe de base depuis une "
|
|
|
|
|
"classe dérivée qui la surcharge?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1626
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you're using new-style classes, use the built-in :func:`super` function::"
|
2017-05-16 21:42:15 +00:00
|
|
|
|
msgstr ""
|
|
|
|
|
"Si vous utilisez des *new-style classes*, Utilisez la fonction native :func:"
|
|
|
|
|
"`super`::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1632
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you're using classic classes: For a class definition such as ``class "
|
|
|
|
|
"Derived(Base): ...`` you can call method ``meth()`` defined in ``Base`` (or "
|
|
|
|
|
"one of ``Base``'s base classes) as ``Base.meth(self, arguments...)``. Here, "
|
|
|
|
|
"``Base.meth`` is an unbound method, so you need to provide the ``self`` "
|
|
|
|
|
"argument."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1640
|
|
|
|
|
msgid "How can I organize my code to make it easier to change the base class?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment puis-je organiser mon code pour permettre de changer la classe de "
|
|
|
|
|
"base plus facilement?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1642
|
|
|
|
|
msgid ""
|
|
|
|
|
"You could define an alias for the base class, assign the real base class to "
|
|
|
|
|
"it before your class definition, and use the alias throughout your class. "
|
|
|
|
|
"Then all you have to change is the value assigned to the alias. "
|
|
|
|
|
"Incidentally, this trick is also handy if you want to decide dynamically (e."
|
|
|
|
|
"g. depending on availability of resources) which base class to use. "
|
|
|
|
|
"Example::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Vous pouvez définir un alias pour la classe de base, lui attribuer la classe "
|
|
|
|
|
"de base réelle avant la définition de classe, et utiliser l'alias au long de "
|
|
|
|
|
"votre classe. Ensuite, tout ce que vous devez changer est la valeur "
|
|
|
|
|
"attribuée à l'alias. Incidemment, cette astuce est également utile si vous "
|
|
|
|
|
"voulez décider dynamiquement (par exemple en fonction de la disponibilité "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"des ressources) la classe de base à utiliser. Exemple ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1657
|
|
|
|
|
msgid "How do I create static class data and static class methods?"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Comment puis-je créer des données statiques de classe et des méthodes "
|
|
|
|
|
"statiques de classe?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1659
|
|
|
|
|
msgid ""
|
|
|
|
|
"Both static data and static methods (in the sense of C++ or Java) are "
|
|
|
|
|
"supported in Python."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Tant les données statiques que les méthodes statiques (dans le sens de C + + "
|
|
|
|
|
"ou Java) sont pris en charge en Python."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1662
|
|
|
|
|
msgid ""
|
|
|
|
|
"For static data, simply define a class attribute. To assign a new value to "
|
|
|
|
|
"the attribute, you have to explicitly use the class name in the assignment::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Pour les données statiques, il suffit de définir un attribut de classe. Pour "
|
|
|
|
|
"attribuer une nouvelle valeur à l'attribut, vous devez explicitement "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"utiliser le nom de classe dans l'affectation ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1674
|
|
|
|
|
msgid ""
|
|
|
|
|
"``c.count`` also refers to ``C.count`` for any ``c`` such that "
|
|
|
|
|
"``isinstance(c, C)`` holds, unless overridden by ``c`` itself or by some "
|
|
|
|
|
"class on the base-class search path from ``c.__class__`` back to ``C``."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"``c.count`` se réfère également à ``C.count`` pour tout ``c`` telle que "
|
|
|
|
|
"``isInstance (c, C)`` est vrai, sauf remplacement par ``c`` lui-même ou par "
|
|
|
|
|
"une classe sur le chemin de recherche de classe de base de ``c.__class__`` "
|
|
|
|
|
"jusqu'à ``C``."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1678
|
|
|
|
|
msgid ""
|
|
|
|
|
"Caution: within a method of C, an assignment like ``self.count = 42`` "
|
|
|
|
|
"creates a new and unrelated instance named \"count\" in ``self``'s own "
|
|
|
|
|
"dict. Rebinding of a class-static data name must always specify the class "
|
|
|
|
|
"whether inside a method or not::"
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Attention: dans une méthode de C, une affectation comme ``self.count = 42`` "
|
|
|
|
|
"crée une nouvelle instance et sans rapport avec le nom ``count`` dans dans "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"le dictionnaire de données de ``self``. La redéfinition d'une donnée "
|
|
|
|
|
"statique de classe doit toujours spécifier la classe que l'on soit à "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"l'intérieur d'une méthode ou non ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1685
|
|
|
|
|
msgid "Static methods are possible since Python 2.2::"
|
|
|
|
|
msgstr "Les méthodes statiques sont possibles depuis Python 2.2::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1693
|
|
|
|
|
msgid "With Python 2.4's decorators, this can also be written as ::"
|
|
|
|
|
msgstr "Avec les décorateurs de Python 2.4, cela peut aussi s'écrire:"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1701
|
|
|
|
|
msgid ""
|
|
|
|
|
"However, a far more straightforward way to get the effect of a static method "
|
|
|
|
|
"is via a simple module-level function::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cependant, d'une manière beaucoup plus simple pour obtenir l'effet d'une "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"méthode statique se fait par une simple fonction au niveau du module ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1707
|
|
|
|
|
msgid ""
|
|
|
|
|
"If your code is structured so as to define one class (or tightly related "
|
|
|
|
|
"class hierarchy) per module, this supplies the desired encapsulation."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Si votre code est structuré de manière à définir une classe (ou bien la "
|
|
|
|
|
"hiérarchie des classes connexes) par module, ceci fournira l'encapsulation "
|
|
|
|
|
"souhaitée."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1712
|
|
|
|
|
msgid "How can I overload constructors (or methods) in Python?"
|
|
|
|
|
msgstr "Comment puis-je surcharger les constructeurs (ou méthodes) en Python?"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1714
|
|
|
|
|
msgid ""
|
|
|
|
|
"This answer actually applies to all methods, but the question usually comes "
|
|
|
|
|
"up first in the context of constructors."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cette réponse s'applique en fait à toutes les méthodes, mais la question "
|
|
|
|
|
"vient généralement en premier dans le contexte des constructeurs."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1717
|
|
|
|
|
msgid "In C++ you'd write"
|
|
|
|
|
msgstr "In C++ you'd write"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1726
|
|
|
|
|
msgid ""
|
|
|
|
|
"In Python you have to write a single constructor that catches all cases "
|
|
|
|
|
"using default arguments. For example::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"En Python, vous devez écrire un constructeur unique qui considère tous les "
|
2018-07-03 09:30:39 +00:00
|
|
|
|
"cas en utilisant des arguments par défaut. Par exemple ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1736
|
|
|
|
|
msgid "This is not entirely equivalent, but close enough in practice."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Ce n'est pas tout à fait équivalent, mais suffisamment proche dans la "
|
|
|
|
|
"pratique."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1738
|
|
|
|
|
msgid "You could also try a variable-length argument list, e.g. ::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Vous pouvez aussi utiliser une liste d'arguments de longueur variable, par "
|
|
|
|
|
"exemple : ::"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1743
|
|
|
|
|
msgid "The same approach works for all method definitions."
|
|
|
|
|
msgstr "La même approche fonctionne pour toutes les définitions de méthode."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1747
|
|
|
|
|
msgid "I try to use __spam and I get an error about _SomeClassName__spam."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"J'essaie d'utiliser ``__spam`` et j'obtiens une erreur à propos de "
|
|
|
|
|
"``_SomeClassName__spam``."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1749
|
|
|
|
|
msgid ""
|
|
|
|
|
"Variable names with double leading underscores are \"mangled\" to provide a "
|
|
|
|
|
"simple but effective way to define class private variables. Any identifier "
|
|
|
|
|
"of the form ``__spam`` (at least two leading underscores, at most one "
|
|
|
|
|
"trailing underscore) is textually replaced with ``_classname__spam``, where "
|
|
|
|
|
"``classname`` is the current class name with any leading underscores "
|
|
|
|
|
"stripped."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Les noms de variables commençant avec deux tirets bas sont «déformés», c'est "
|
|
|
|
|
"un moyen simple mais efficace de définir variables privées à une classe. "
|
|
|
|
|
"Tout identifiant de la forme ``__spam`` (commençant par au moins deux tirets "
|
|
|
|
|
"bas et se terminant par au plus un tiret bas) est textuellement remplacé par "
|
|
|
|
|
"``_classname__spam``, où ``classname`` est le nom de la classe en cours dont "
|
|
|
|
|
"les éventuels tirets bas ont été retirés."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1755
|
|
|
|
|
msgid ""
|
|
|
|
|
"This doesn't guarantee privacy: an outside user can still deliberately "
|
|
|
|
|
"access the \"_classname__spam\" attribute, and private values are visible in "
|
|
|
|
|
"the object's ``__dict__``. Many Python programmers never bother to use "
|
|
|
|
|
"private variable names at all."
|
|
|
|
|
msgstr ""
|
|
|
|
|
"Cela ne garantit pas la privauté de l'accès : un utilisateur extérieur peut "
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"encore délibérément accéder à l'attribut ``_classname__spam``, et les "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"valeurs privées sont visibles dans l'objet ``__dict__``. De nombreux "
|
|
|
|
|
"programmeurs Python ne prennent jamais la peine d'utiliser des noms de "
|
|
|
|
|
"variable privée."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1762
|
|
|
|
|
msgid "My class defines __del__ but it is not called when I delete the object."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"Ma classe définit ``__del__`` mais il n'est pas appelé lorsque je supprime "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"l'objet."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1764
|
|
|
|
|
msgid "There are several possible reasons for this."
|
|
|
|
|
msgstr "Il y a plusieurs raisons possibles pour cela."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1766
|
|
|
|
|
msgid ""
|
|
|
|
|
"The del statement does not necessarily call :meth:`__del__` -- it simply "
|
|
|
|
|
"decrements the object's reference count, and if this reaches zero :meth:"
|
|
|
|
|
"`__del__` is called."
|
|
|
|
|
msgstr ""
|
2018-10-10 16:34:12 +00:00
|
|
|
|
"La commande *del* n'appelle pas forcément :meth:`__del__` --- il décrémente "
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"simplement le compteur de références de l'objet, et si celui ci arrive à "
|
|
|
|
|
"zéro :meth:`__del__` est appelée."
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1770
|
|
|
|
|
msgid ""
|
|
|
|
|
"If your data structures contain circular links (e.g. a tree where each child "
|
|
|
|
|
"has a parent reference and each parent has a list of children) the reference "
|
|
|
|
|
"counts will never go back to zero. Once in a while Python runs an algorithm "
|
|
|
|
|
"to detect such cycles, but the garbage collector might run some time after "
|
|
|
|
|
"the last reference to your data structure vanishes, so your :meth:`__del__` "
|
|
|
|
|
"method may be called at an inconvenient and random time. This is "
|
|
|
|
|
"inconvenient if you're trying to reproduce a problem. Worse, the order in "
|
|
|
|
|
"which object's :meth:`__del__` methods are executed is arbitrary. You can "
|
|
|
|
|
"run :func:`gc.collect` to force a collection, but there *are* pathological "
|
|
|
|
|
"cases where objects will never be collected."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Si la structure de données contient des références circulaires (e.g. un "
|
|
|
|
|
"arbre dans lequel chaque fils référence son père, et chaque père garde une "
|
|
|
|
|
"liste de ses fils), le compteur de références n'arrivera jamais à zéro. "
|
|
|
|
|
"Python exécute périodiquement un algorithme pour détecter ce genre de "
|
|
|
|
|
"cycles, mais il peut se passer un certain temps entre le moment où la "
|
|
|
|
|
"structure est référencée pour la dernière fois et l'appel du ramasse-"
|
|
|
|
|
"miettes, donc la méthode :meth:`__del__` peut être appelée à un moment "
|
|
|
|
|
"aléatoire et pas opportun. C'est gênant pour essayer reproduire un problème. "
|
|
|
|
|
"Pire, l'ordre dans lequel les méthodes :meth:`__del__` des objets sont "
|
|
|
|
|
"appelées est arbitraire. Il est possible de forcer l'appel du ramasse-"
|
|
|
|
|
"miettes avec la fonction :func:`gc.collect`, mais il existe certains cas où "
|
|
|
|
|
"les objets ne seront jamais nettoyés."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1781
|
|
|
|
|
msgid ""
|
|
|
|
|
"Despite the cycle collector, it's still a good idea to define an explicit "
|
|
|
|
|
"``close()`` method on objects to be called whenever you're done with them. "
|
|
|
|
|
"The ``close()`` method can then remove attributes that refer to subobjecs. "
|
|
|
|
|
"Don't call :meth:`__del__` directly -- :meth:`__del__` should call "
|
|
|
|
|
"``close()`` and ``close()`` should make sure that it can be called more than "
|
|
|
|
|
"once for the same object."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1788
|
|
|
|
|
msgid ""
|
|
|
|
|
"Another way to avoid cyclical references is to use the :mod:`weakref` "
|
|
|
|
|
"module, which allows you to point to objects without incrementing their "
|
|
|
|
|
"reference count. Tree data structures, for instance, should use weak "
|
|
|
|
|
"references for their parent and sibling references (if they need them!)."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Une alternative pour éviter les références cycliques consiste à utiliser le "
|
|
|
|
|
"module :mod:`weakref`, qui permet de faire référence à des objets sans "
|
|
|
|
|
"incrémenter leur compteur de références. Par exemple, les structures "
|
|
|
|
|
"d'arbres devraient utiliser des références faibles entre pères et fils (si "
|
|
|
|
|
"nécessaire !)."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1793
|
|
|
|
|
msgid ""
|
|
|
|
|
"If the object has ever been a local variable in a function that caught an "
|
|
|
|
|
"expression in an except clause, chances are that a reference to the object "
|
|
|
|
|
"still exists in that function's stack frame as contained in the stack trace. "
|
|
|
|
|
"Normally, calling :func:`sys.exc_clear` will take care of this by clearing "
|
|
|
|
|
"the last recorded exception."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1799
|
|
|
|
|
msgid ""
|
|
|
|
|
"Finally, if your :meth:`__del__` method raises an exception, a warning "
|
|
|
|
|
"message is printed to :data:`sys.stderr`."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Enfin, si la méthode :meth:`__del__` lève une exception, un message "
|
|
|
|
|
"d'avertissement s'affiche dans :data:`sys.stderr`."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1804
|
|
|
|
|
msgid "How do I get a list of all instances of a given class?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Comment obtenir toutes les instances d'une classe ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1806
|
|
|
|
|
msgid ""
|
|
|
|
|
"Python does not keep track of all instances of a class (or of a built-in "
|
|
|
|
|
"type). You can program the class's constructor to keep track of all "
|
|
|
|
|
"instances by keeping a list of weak references to each instance."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Python ne tient pas de registre de toutes les instances d'une classe (ni de "
|
|
|
|
|
"n'importe quel type natif). Il est cependant possible de programmer le "
|
|
|
|
|
"constructeur de la classe de façon à tenir un tel registre, en maintenant "
|
|
|
|
|
"une liste de références faibles vers chaque instance."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1812
|
|
|
|
|
msgid "Why does the result of ``id()`` appear to be not unique?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Pourquoi le résultat de ``id()`` peut-il être le même pour deux objets "
|
|
|
|
|
"différents ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1814
|
|
|
|
|
msgid ""
|
|
|
|
|
"The :func:`id` builtin returns an integer that is guaranteed to be unique "
|
|
|
|
|
"during the lifetime of the object. Since in CPython, this is the object's "
|
|
|
|
|
"memory address, it happens frequently that after an object is deleted from "
|
|
|
|
|
"memory, the next freshly created object is allocated at the same position in "
|
|
|
|
|
"memory. This is illustrated by this example:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"La fonction native :func:`id` renvoie un entier dont l'unicité est garantie "
|
|
|
|
|
"durant toute la vie de l'objet. Vu qu'en CPython cet entier est en réalité "
|
|
|
|
|
"l'adresse mémoire de l'objet, il est fréquent qu'un nouvel objet soit alloué "
|
|
|
|
|
"à une adresse mémoire identique à celle d'un objet venant d'être supprimé. "
|
|
|
|
|
"Comme l'illustre le code suivant :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1825
|
|
|
|
|
msgid ""
|
|
|
|
|
"The two ids belong to different integer objects that are created before, and "
|
|
|
|
|
"deleted immediately after execution of the ``id()`` call. To be sure that "
|
|
|
|
|
"objects whose id you want to examine are still alive, create another "
|
|
|
|
|
"reference to the object:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Les deux identifiants appartiennent à des objets entiers créés juste avant "
|
|
|
|
|
"l'appel à ``id()`` et détruits immédiatement après. Pour s'assurer que les "
|
|
|
|
|
"objets dont on veut examiner les identifiants sont toujours en vie, créons "
|
|
|
|
|
"une nouvelle référence à l'objet :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1838
|
|
|
|
|
msgid "Modules"
|
|
|
|
|
msgstr "Modules"
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1841
|
|
|
|
|
msgid "How do I create a .pyc file?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Comment créer des fichiers ``.pyc`` ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1843
|
|
|
|
|
msgid ""
|
|
|
|
|
"When a module is imported for the first time (or when the source is more "
|
|
|
|
|
"recent than the current compiled file) a ``.pyc`` file containing the "
|
|
|
|
|
"compiled code should be created in the same directory as the ``.py`` file."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1847
|
|
|
|
|
msgid ""
|
|
|
|
|
"One reason that a ``.pyc`` file may not be created is permissions problems "
|
|
|
|
|
"with the directory. This can happen, for example, if you develop as one user "
|
|
|
|
|
"but run as another, such as if you are testing with a web server. Creation "
|
|
|
|
|
"of a .pyc file is automatic if you're importing a module and Python has the "
|
|
|
|
|
"ability (permissions, free space, etc...) to write the compiled module back "
|
|
|
|
|
"to the directory."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1854
|
|
|
|
|
msgid ""
|
|
|
|
|
"Running Python on a top level script is not considered an import and no ``."
|
|
|
|
|
"pyc`` will be created. For example, if you have a top-level module ``foo."
|
|
|
|
|
"py`` that imports another module ``xyz.py``, when you run ``foo``, ``xyz."
|
|
|
|
|
"pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file "
|
|
|
|
|
"will be created since ``foo.py`` isn't being imported."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1860
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for "
|
|
|
|
|
"a module that is not imported -- you can, using the :mod:`py_compile` and :"
|
|
|
|
|
"mod:`compileall` modules."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1864
|
|
|
|
|
msgid ""
|
|
|
|
|
"The :mod:`py_compile` module can manually compile any module. One way is to "
|
|
|
|
|
"use the ``compile()`` function in that module interactively::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Le module :mod:`py_compile` peut compiler n'importe quel module "
|
|
|
|
|
"manuellement. Il est ainsi possible d'appeler la fonction ``compile()`` de "
|
|
|
|
|
"manière interactive :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1870
|
|
|
|
|
msgid ""
|
|
|
|
|
"This will write the ``.pyc`` to the same location as ``foo.py`` (or you can "
|
|
|
|
|
"override that with the optional parameter ``cfile``)."
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1873
|
|
|
|
|
msgid ""
|
|
|
|
|
"You can also automatically compile all files in a directory or directories "
|
|
|
|
|
"using the :mod:`compileall` module. You can do it from the shell prompt by "
|
|
|
|
|
"running ``compileall.py`` and providing the path of a directory containing "
|
|
|
|
|
"Python files to compile::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Tous les fichiers d'un ou plusieurs dossiers peuvent aussi être compilés "
|
|
|
|
|
"avec le module :mod:`compileall`. C'est possible depuis l'invite de commande "
|
|
|
|
|
"en exécutant ``compileall.py`` avec le chemin du dossier contenant les "
|
|
|
|
|
"fichiers Python à compiler ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1882
|
|
|
|
|
msgid "How do I find the current module name?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Comment obtenir le nom du module actuel ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1884
|
|
|
|
|
msgid ""
|
|
|
|
|
"A module can find out its own module name by looking at the predefined "
|
|
|
|
|
"global variable ``__name__``. If this has the value ``'__main__'``, the "
|
|
|
|
|
"program is running as a script. Many modules that are usually used by "
|
|
|
|
|
"importing them also provide a command-line interface or a self-test, and "
|
|
|
|
|
"only execute this code after checking ``__name__``::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Un module peut déterminer son propre nom en examinant la variable globale "
|
|
|
|
|
"prédéfinie ``__name__``. Si celle-ci vaut ``'__main__'``, c'est que le "
|
|
|
|
|
"programme est exécuté comme un script. Beaucoup de modules qui doivent "
|
|
|
|
|
"normalement être importés pour pouvoir être utilisés fournissent aussi une "
|
|
|
|
|
"interface en ligne de commande ou un test automatique. Ils n'exécutent cette "
|
|
|
|
|
"portion du code qu'après avoir vérifié la valeur de ``__name__``::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1899
|
|
|
|
|
msgid "How can I have modules that mutually import each other?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Comment avoir des modules qui s'importent mutuellement ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1901
|
|
|
|
|
msgid "Suppose you have the following modules:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Considérons les modules suivants :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1903
|
|
|
|
|
msgid "foo.py::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*foo.py* ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1908
|
|
|
|
|
msgid "bar.py::"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*bar.py* ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1913
|
|
|
|
|
msgid "The problem is that the interpreter will perform the following steps:"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Le problème réside dans les étapes que l'interpréteur va réaliser :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1915
|
|
|
|
|
msgid "main imports foo"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*main* importe *foo*"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1916
|
|
|
|
|
msgid "Empty globals for foo are created"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Les variables globales (vides) de *foo* sont créées"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1917
|
|
|
|
|
msgid "foo is compiled and starts executing"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*foo* est compilé et commence à s'exécuter"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1918
|
|
|
|
|
msgid "foo imports bar"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*foo* importe *bar*"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1919
|
|
|
|
|
msgid "Empty globals for bar are created"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Les variables globales (vides) de *bar* sont créées"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1920
|
|
|
|
|
msgid "bar is compiled and starts executing"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "*bar* est compilé et commence à s'exécuter"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1921
|
|
|
|
|
msgid ""
|
|
|
|
|
"bar imports foo (which is a no-op since there already is a module named foo)"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"*bar* importe *foo* (en réalité, rien ne passe car il y a déjà un module "
|
|
|
|
|
"appelé *foo*)"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1922
|
|
|
|
|
msgid "bar.foo_var = foo.foo_var"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "bar.foo_var = foo.foo_var"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1924
|
|
|
|
|
msgid ""
|
|
|
|
|
"The last step fails, because Python isn't done with interpreting ``foo`` yet "
|
|
|
|
|
"and the global symbol dictionary for ``foo`` is still empty."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"La dernière étape échoue car Python n'a pas fini d'interpréter ``foo`` et le "
|
|
|
|
|
"dictionnaire global des symboles de ``foo`` est encore vide."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1927
|
|
|
|
|
msgid ""
|
|
|
|
|
"The same thing happens when you use ``import foo``, and then try to access "
|
|
|
|
|
"``foo.foo_var`` in global code."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Le même phénomène arrive quand on utilise ``import foo``, et qu'on essaye "
|
|
|
|
|
"ensuite d'accéder à ``foo.foo_var`` dans le code global."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1930
|
|
|
|
|
msgid "There are (at least) three possible workarounds for this problem."
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Il y a (au moins) trois façons de contourner ce problème."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1932
|
|
|
|
|
msgid ""
|
|
|
|
|
"Guido van Rossum recommends avoiding all uses of ``from <module> import ..."
|
|
|
|
|
"``, and placing all code inside functions. Initializations of global "
|
|
|
|
|
"variables and class variables should use constants or built-in functions "
|
|
|
|
|
"only. This means everything from an imported module is referenced as "
|
|
|
|
|
"``<module>.<name>``."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Guido van Rossum déconseille d'utiliser ``from <module> import ...`` et de "
|
|
|
|
|
"mettre tout le code dans des fonctions. L'initialisation des variables "
|
|
|
|
|
"globales et des variables de classe ne doit utiliser que des constantes ou "
|
|
|
|
|
"des fonctions natives. Ceci implique que tout ce qui est fourni par un "
|
|
|
|
|
"module soit référencé par ``<module>.<nom>``."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1937
|
|
|
|
|
msgid ""
|
|
|
|
|
"Jim Roskind suggests performing steps in the following order in each module:"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Jim Roskind recommande d'effectuer les étapes suivantes dans cet ordre dans "
|
|
|
|
|
"chaque module :"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1939
|
|
|
|
|
msgid ""
|
|
|
|
|
"exports (globals, functions, and classes that don't need imported base "
|
|
|
|
|
"classes)"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"les exportations (variables globales, fonctions et les classes qui ne "
|
|
|
|
|
"nécessitent d'importer des classes de base)"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1941
|
|
|
|
|
msgid "``import`` statements"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "les instructions ``import``"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1942
|
|
|
|
|
msgid ""
|
|
|
|
|
"active code (including globals that are initialized from imported values)."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"le code (avec les variables globales qui sont initialisées à partir de "
|
|
|
|
|
"valeurs importées)."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1944
|
|
|
|
|
msgid ""
|
|
|
|
|
"van Rossum doesn't like this approach much because the imports appear in a "
|
|
|
|
|
"strange place, but it does work."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"van Rossum désapprouve cette approche car les importations se trouvent à un "
|
|
|
|
|
"endroit bizarre, mais cela fonctionne."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1947
|
|
|
|
|
msgid ""
|
|
|
|
|
"Matthias Urlichs recommends restructuring your code so that the recursive "
|
|
|
|
|
"import is not necessary in the first place."
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Matthias Urlichs conseille de restructurer le code pour éviter les "
|
|
|
|
|
"importations récursives."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1950
|
|
|
|
|
msgid "These solutions are not mutually exclusive."
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "Ces solutions peuvent être combinées."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1954
|
|
|
|
|
msgid "__import__('x.y.z') returns <module 'x'>; how do I get z?"
|
2019-12-06 13:21:00 +00:00
|
|
|
|
msgstr "__import__('x.y.z') renvoie <module 'x'> ; comment accéder à z ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1956
|
|
|
|
|
msgid ""
|
|
|
|
|
"Consider using the convenience function :func:`~importlib.import_module` "
|
|
|
|
|
"from :mod:`importlib` instead::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Utilisez plutôt la fonction :func:`~importlib.import_module` de :mod:"
|
|
|
|
|
"`importlib` ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1963
|
|
|
|
|
msgid ""
|
|
|
|
|
"When I edit an imported module and reimport it, the changes don't show up. "
|
|
|
|
|
"Why does this happen?"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Quand j'édite un module et que je le réimporte, je ne vois pas les "
|
|
|
|
|
"changements. Pourquoi ?"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1965
|
|
|
|
|
msgid ""
|
|
|
|
|
"For reasons of efficiency as well as consistency, Python only reads the "
|
|
|
|
|
"module file on the first time a module is imported. If it didn't, in a "
|
|
|
|
|
"program consisting of many modules where each one imports the same basic "
|
|
|
|
|
"module, the basic module would be parsed and re-parsed many times. To force "
|
|
|
|
|
"rereading of a changed module, do this::"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1974
|
|
|
|
|
msgid ""
|
|
|
|
|
"Warning: this technique is not 100% fool-proof. In particular, modules "
|
|
|
|
|
"containing statements like ::"
|
|
|
|
|
msgstr ""
|
2019-12-06 13:21:00 +00:00
|
|
|
|
"Attention, cette technique ne marche pas systématiquement. En particulier, "
|
|
|
|
|
"les modules qui contiennent des instructions comme ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1979
|
|
|
|
|
msgid ""
|
|
|
|
|
"will continue to work with the old version of the imported objects. If the "
|
|
|
|
|
"module contains class definitions, existing class instances will *not* be "
|
|
|
|
|
"updated to use the new class definition. This can result in the following "
|
|
|
|
|
"paradoxical behaviour:"
|
|
|
|
|
msgstr ""
|
|
|
|
|
|
|
|
|
|
#: ../Doc/faq/programming.rst:1991
|
|
|
|
|
msgid ""
|
|
|
|
|
"The nature of the problem is made clear if you print out the class objects:"
|
|
|
|
|
msgstr ""
|