|
Original |
Translation |
|
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
|
The output of the example code is::
|
Ce code donne le résultat suivant ::
|
|
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
|
|
51
|
Classes introduce a little bit of new syntax, three new object types, and some new semantics.
|
Le concept de classes introduit quelques nouveau éléments de syntaxe, trois nouveaux types d'objets ainsi que de nouveaux éléments de sémantique
|
|
52
|
|
Syntaxe de définition des classes
|
|
53
|
The simplest form of class definition looks like this::
|
La forme la plus simple de définition de classe ressemble à ceci ::
|
|
54
|
class ClassName: <statement-1> . . . <statement-N>
|
class NomDeLaClasse: <déclaration-1>. . . <déclaration-N>
|