Advertisement
Fhernd

r712_acceso_variables_closure.py

Nov 24th, 2018
2,781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.37 KB | None | 0 0
  1. def fn():
  2.     n = 0
  3.  
  4.     def fn_closure():
  5.         print('n = {}'.format(n))
  6.  
  7.     def obtener_n():
  8.         return n
  9.  
  10.     def establecer_n(valor):
  11.         nonlocal n
  12.         n = valor
  13.  
  14.     fn_closure.obtener_n = obtener_n
  15.     fn_closure.establecer_n = establecer_n
  16.  
  17.     return fn_closure
  18.  
  19.  
  20. f = fn()
  21. f()
  22. f.establecer_n(10)
  23. f()
  24. n = f.obtener_n()
  25. print(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement