Original Translation
77
Valid method names of an instance object depend on its class. By definition, all attributes of a class that are function objects define corresponding methods of its instances. So in our example, ``x.f`` is a valid method reference, since ``MyClass.f`` is a function, but ``x.i`` is not, since ``MyClass.i`` is not. But ``x.f`` is not the same thing as ``MyClass.f`` --- it is a *method object*, not a function object.
Les noms de méthodes valides d'un objet instance dépendent de sa classe. Par définition, tous les attributs d'une classe qui sont des objets fonction définissent les méthodes correspondantes de ses instances. Donc, dans notre exemple, ``x.f`` est une méthode de référence valide, car ``MaClasse.f`` est une fonction, mais pas ``x.i`` car ``MaClasse.i`` n'en est pas une. Attention cependant, ``x.f`` n'est pas la même chose que ``MaClasse.f`` --- Il s'agit d'un *objet méthode*, pas d'un objet fonction.
78
Method Objects
Les objets méthode
79
Usually, a method is called right after it is bound::
80
x.f()
81
In the :class:`MyClass` example, this will return the string ``'hello world'``. However, it is not necessary to call a method right away: ``x.f`` is a method object, and can be stored away and called at a later time. For example::
82
xf = x.f while True: print(xf())
xf = x.f while True: print(xf())
83
will continue to print ``hello world`` until the end of time.
va afficher ``hello world`` jusqu'à la fin des temps.
84
What exactly happens when a method is called? You may have noticed that ``x.f()`` was called without an argument above, even though the function definition for :meth:`f` specified an argument. What happened to the argument? Surely Python raises an exception when a function that requires an argument is called without any --- even if the argument isn't actually used...
Que ce passe-t-il exactement quand une méthode est appelé ? Vous avez du remarqué que ``x.f()`` a été appelé dans le code au dessus sans argument, alors que la définition de la méthode :meth: `f` spécifie bien qu'elle prend un argument. Qu'est il arrivé à l'argument ? Python doit surement lever une exception lorsqu'une fonction qui requiert un argument est appelé sans -- même si l'argument n'est pas utilisé.
85
Actually, you may have guessed the answer: the special thing about methods is that the object is passed as the first argument of the function. In our example, the call ``x.f()`` is exactly equivalent to ``MyClass.f(x)``. In general, calling a method with a list of *n* arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method's object before the first argument.
En fait, vous aurez peut-être deviné la réponse: la particularité des méthodes est que l'objet est passé comme premier argument de la fonction. Dans notre exemple, l'appel ``x.f ()`` est exactement équivalent à ``MaClasse.f(x)``. En général, appeler une méthode avec une liste d'arguments *n* est équivalent à appeler la fonction correspondante avec cette liste d'arguments modulo l'insertion de l'objet de la méthode avant le premier argument.
86
If you still don't understand how methods work, a look at the implementation can perhaps clarify matters. When an instance attribute is referenced that isn't a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object. When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list.
Si vous ne comprenez toujours pas comment les méthodes fonctionnent, un coup d'œil à l'implémentation vous aidera peut être. Lorsque l'instance d'un attribut est référencé qui n'ai pas un attribut donnée, sa classe est recherché. Si le nom de la classe correspond à un attribut valide qui est une fonction, alors un objet abstrait qui pointe vers l'objet instance et l'objet fonction que l'on a trouvé. Cette objet abstrait est l'objet méthode. Lorsque l'objet méthode est appelé avec une liste d'argument, une nouvelle liste d'argument est construite à partir de l'objet méthode et de la liste des argument. L'objet fonction est appelé avec cette nouvelle liste d'argument.