6b4436cmodule-10(ex4): remove global var
1 file changed, +11, -8
author
mogi <git@mogiya.dev> on 27-05-2026 23:51 UTC+1
committer
mogi <git@mogiya.dev> on 27-05-2026 23:51 UTC+1
parentc80f6a3
      @@ -79,15 +79,18 @@ def failed_spell() -> str:
 79 79     raise RuntimeError("Spell misfired!")
 80 80 
 81 81 
 82   -_counter = [0]
    82+def _make_eventual_spell() -> Callable:
    83+    counter = 0
 83 84 
    85+    @retry_spell(3)
    86+    def eventual_spell() -> str:
    87+        nonlocal counter
    88+        counter += 1
    89+        if counter < 3:
    90+            raise RuntimeError("Not yet...")
    91+        return "Waaaaaagh spelled !"
 84 92 
 85   -@retry_spell(3)
 86   -def eventual_spell() -> str:
 87   -    _counter[0] += 1
 88   -    if _counter[0] < 3:
 89   -        raise RuntimeError("Not yet...")
 90   -    return "Waaaaaagh spelled !"
    93+    return eventual_spell
 91 94 
 92 95 
 93 96 if __name__ == "__main__":
      @@ -96,7 +99,7 @@ if __name__ == "__main__":
 96 99 
 97100     print("\nTesting retrying spell...")
 98101     print(failed_spell())
 99   -    print(eventual_spell())
   102+    print(_make_eventual_spell()())
100103 
101104     print("\nTesting MageGuild...")
102105     guild = MageGuild()