Original Translation
64
Class *instantiation* uses function notation. Just pretend that the class object is a parameterless function that returns a new instance of the class. For example (assuming the above class)::
L'*instanciation* de classes utilise la notation des fonctions. Considérez simplement que l'objet classe est une fonction sans paramètre qui renvoie une nouvelle instance de la classe. Par exemple (en considérant la classe définie ci-dessus) ::
65
x = MyClass()
x = MaClasse()
66
creates a new *instance* of the class and assigns this object to the local variable ``x``.
crée une nouvelle *instance* de la classe et affecte cet objet à la variable locale ``x``.
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::
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
>>> class Complex: ... def __init__(self, realpart, imagpart): ... self.r = realpart ... self.i = imagpart ... >>> x = Complex(3.0, -4.5) >>> x.r, x.i (3.0, -4.5)
>>> class Complexe: ... def __init__(self, partie_reelle, partie_imaginaire): ... self.r = partie_reelle ... self.i = partie_imaginaire ... >>> x = Complexe(3.0, -4.5) >>> x.r, x.i (3.0, -4.5)
72
Instance Objects
Objets instance
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.