|
Original |
Translation |
|
41
|
A special quirk of Python is that -- if no :keyword:`global` statement is in effect -- assignments to names always go into the innermost scope. Assignments do not copy data --- they just bind names to objects. The same is true for deletions: the statement ``del x`` removes the binding of ``x`` from the namespace referenced by the local scope. In fact, all operations that introduce new names use the local scope: in particular, :keyword:`import` statements and function definitions bind the module or function name in the local scope.
|
Une particularité de Python est que si aucune instruction :keyword:`global` n'est active, les affectations de noms vont toujours dans la portée la plus proche. Les affectations ne copient aucune données : elles se contentent de lier des noms à des objets. Ceci est également vrai pour l'effacement : l'instruction ``del x`` supprime la liaison de ``x`` dans l'espace de noms référencé par la portée locale. En réalité, toutes les opérations qui impliquent des nouveaux noms utilisent la portée locale : en particulier, les instructions keyword:`import` et les définitions de fonctions effectuent une liaison du module ou du nom de fonction dans la portée locale.
|
|
42
|
The :keyword:`global` statement can be used to indicate that particular variables live in the global scope and should be rebound there; the :keyword:`nonlocal` statement indicates that particular variables live in an enclosing scope and should be rebound there.
|
L'instruction :keyword:`global` peut être utilisée pour indiquer que certaines variables existent dans la portée globale et doivent être reliées en local ; l'instruction :keyword:`nonlocal` indique que certaines variables existent dans une portée supérieure et doivent être reliées en local.
|
|
43
|
Scopes and Namespaces Example
|
Exemple de portées et d'espaces de noms
|
|
44
|
This is an example demonstrating how to reference the different scopes and namespaces, and how :keyword:`global` and :keyword:`nonlocal` affect variable binding::
|
Ceci est un exemple montrant comment utiliser les différentes portées et espaces de noms, et comment :keyword:`global` et :keyword:`nonlocal` modifient l'affectation de variable ::
|
|
45
|
def scope_test(): def do_local(): spam = "local spam" def do_nonlocal(): nonlocal spam spam = "nonlocal spam" def do_global(): global spam spam = "global spam" spam = "test spam" do_local() print("After local assignment:", spam) do_nonlocal() print("After nonlocal assignment:", spam) do_global() print("After global assignment:", spam) scope_test() print("In global scope:", spam)
|
def scope_test(): def do_local(): spam = "local spam" def do_nonlocal(): nonlocal spam spam = "nonlocal spam" def do_global(): global spam spam = "global spam" spam = "test spam" do_local() print("After local assignment:", spam) do_nonlocal() print("After nonlocal assignment:", spam) do_global() print("After global assignment:", spam) scope_test() print("In global scope:", spam)
|
|
46
|
|
47
|
After local assignment: test spam After nonlocal assignment: nonlocal spam After global assignment: nonlocal spam In global scope: global spam
|
After local assignment: test spam After nonlocal assignment: nonlocal spam After global assignment: nonlocal spam In global scope: global spam
|
|
48
|
Note how the *local* assignment (which is default) didn't change *scope_test*\'s binding of *spam*. The :keyword:`nonlocal` assignment changed *scope_test*\'s binding of *spam*, and the :keyword:`global` assignment changed the module-level binding.
|
Vous pouvez constater que l'affectation *locale* (qui est effectuée par défaut) n'a pas modifié la liaison de *spam* dans *scope_test*. L'affectation :keyword:`nonlocal` a changé la liaison de *spam* dans *scope_test* et l'affectation :keyword:`global` a changé la liaison au niveau du module.
|
|
49
|
You can also see that there was no previous binding for *spam* before the :keyword:`global` assignment.
|
Vous pouvez également voir qu'aucune liaison pour *spam* n'a été faite avant l'affectation :keyword:`global`.
|
|
50
|
|
Une première approche des classes
|