|
Original |
Translation |
|
67
|
The instantiation operation ("calling" a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named :meth:`__init__`, like this::
|
L'opération d'instanciation ("appelant" un objet classe) crée un objet vide. De nombreuses classes aiment créer des objets personnalisés avec des instances personnalisées en fonction d'un état initial spécifique. Ainsi une classe peut définir une méthode spéciale nommée: meth:`__init__`, comme ceci ::
|
|
68
|
def __init__(self): self.data = []
|
def __init__(self): self.data = []
|
|
69
|
When a class defines an :meth:`__init__` method, class instantiation automatically invokes :meth:`__init__` for the newly-created class instance. So in this example, a new, initialized instance can be obtained by::
|
Quand une classe définit une méthode :meth:`__init__`, l'instanciation de la classe appelle automatiquement :meth:`__init__` pour la nouvelle instance de la classe. Donc, dans cet exemple, l'initialisation d'une nouvelle instance peut être obtenue par ::
|
|
70
|
Of course, the :meth:`__init__` method may have arguments for greater flexibility. In that case, arguments given to the class instantiation operator are passed on to :meth:`__init__`. For example, ::
|
Bien sûr, la méthode meth::`__init__` peut avoir des arguments pour une plus grande flexibilité. Dans ce cas, les arguments donnés à l'opérateur d'instanciation de classe sont transmis à :meth:`__init__`. Par exemple, ::
|
|
71
|
|
72
|
|
|
|
73
|
Now what can we do with instance objects? The only operations understood by instance objects are attribute references. There are two kinds of valid attribute names, data attributes and methods.
|
Maintenant, que pouvons-nous faire avec des objets instance ? Les seules opérations comprises par objets instance sont des références d'attribut. Il y a deux sortes de noms d'attributs valides, les attributs données et les méthodes.
|
|
74
|
*data attributes* correspond to "instance variables" in Smalltalk, and to "data members" in C++. Data attributes need not be declared; like local variables, they spring into existence when they are first assigned to. For example, if ``x`` is the instance of :class:`MyClass` created above, the following piece of code will print the value ``16``, without leaving a trace::
|
Les *attributs données* correspondent à des "variables d'instance" en Smalltalk, et aux "membres de données" en C++. Les attributs données n'ont pas à être déclarés. Comme les variables locales, ils existent dès lors qu'ils sont attribués une première fois. Par exemple, si ``x`` est l'instance de :class:`MaClasse` créée ci-dessus, le code suivant affiche la valeur ``16 ``, sans laisser de traces ::
|
|
75
|
x.counter = 1 while x.counter < 10: x.counter = x.counter * 2 print(x.counter) del x.counter
|
x.compteur = 1 while x.compteur < 10: x.compteur = x.compteur * 2 print(x.compteur) del x.compteur
|
|
76
|
The other kind of instance attribute reference is a *method*. A method is a function that "belongs to" an object. (In Python, the term method is not unique to class instances: other object types can have methods as well. For example, list objects have methods called append, insert, remove, sort, and so on. However, in the following discussion, we'll use the term method exclusively to mean methods of class instance objects, unless explicitly stated otherwise.)
|
L'autre type de référence à un attribut d'instance est une *méthode*. Une méthode est une fonction qui "appartient à" un objet. (En Python, le terme de méthode n'est pas unique aux instances de classe: d'autres types d'objets peuvent aussi avoir des méthodes. Par exemple, les objets liste ont des méthodes appelées append, insert, remove, sort, et ainsi de suite. Toutefois, dans la discussion qui suit, sauf indication contraire, nous allons utiliser le terme de méthode exclusivement en référence à des méthodes d'objets instance de classe.)
|